将 System::Datetime 转换为 QDateTime

Convert System::Datetime to QDateTime

如何转换 System::Datetime to QDateTime

也许这是一种简单的方法,但它很容易被低估:

a) 使用 DateTime.ToString()

将 System::Datetime 转换为字符串

b) 使用 QDateTime::fromString()

将字符串转换为 QDateTime

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

使用 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

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

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

请参阅 QDateTime::fromString and DateTime::ToString 文档

QDateTime::fromString(QString::fromStdWString(msclr::interop::marshal_as<std::wstring>(systemDateTime.ToString("ddMMyyyy HH:mm:ss"))), "ddMMyyyy HH:mm:ss"))