将System :: Datetime转换为QDateTime

时间:2016-04-01 09:19:03

标签: c++ .net qt qdatetime

如何将System::Datetime转换为QDateTime

4 个答案:

答案 0 :(得分:0)

也许这是一种简单明了的方法,但很容易理解:

a)将System :: Datetime转换为DateTime.ToString()

的字符串

b)用QDateTime::fromString()

将字符串转换为QDateTime

或考虑使用QDateTime::fromTime_t()(请参阅Datetime to time_t conversion的示例)

答案 1 :(得分:0)

使用Win32 API

#define WINDOWS_TICKS_PER_SEC 10000000
#define EPOCH_DIFFERENCE 11644473600LL

QDatetime getQDatetime(){
    FILETIME ft = {0}; 
    ::GetSystemTimeAsFileTime(&ft);  //Retrieves the current system date and time.
    LARGE_INTEGER li = {0};
    li.LowPart = ft.dwLowDateTime;
    li.HighPart = ft.dwHighDateTime;
    long long int hns = li.QuadPart;
    wprintf(L"Windows API time: %lli\n", hns);
    long long int utm ;
    utm=(hns / WINDOWS_TICKS_PER_SEC - EPOCH_DIFFERENCE);
    wprintf(L"Unix time: %lli\n", utm);
    return QDateTime::fromTime_t(utm);
}

See this for more help

答案 2 :(得分:0)

您可以使用 ISO 8601 规范作为中间格式:

/* not tested */
System::DateTime date;
QString str(date.ToString("O")); // "O" for ISO format
QDateTime qdt = QDateTime::fromString(str, Qt::ISODate);

请参阅QDateTime::fromStringDateTime::ToString文档

答案 3 :(得分:0)

{{1}}