How can I convert System::Datetime to QDateTime?
Asked
Active
Viewed 2,334 times
1
-
You may want to take a look at [this question](http://stackoverflow.com/questions/4030511/convert-a-qdatetime-in-utc-to-local-system-time) and [this answer](http://stackoverflow.com/a/30930959/5653461) from another question. – IAmInPLS Apr 01 '16 at 09:32
-
2Possible duplicate of [QDateTime Conversion](http://stackoverflow.com/questions/30930085/qdatetime-conversion) – kawashita86 Apr 01 '16 at 09:47
4 Answers
0
Perhaps this is straightforward method, but it can be easy for understating:
a) convert System::Datetime to string with DateTime.ToString()
b) convert string to QDateTime with QDateTime::fromString()
Or consider usage of QDateTime::fromTime_t()
(see example of Datetime to time_t conversion)
0
using 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);
}

Ankur
- 1,385
- 11
- 21
-
Convert Windows Filetime to second in Unix/Linux : http://stackoverflow.com/a/6161842/2731967 – Ankur Apr 01 '16 at 10:42
0
You can use the ISO 8601 norm as intermediate format :
/* not tested */
System::DateTime date;
QString str(date.ToString("O")); // "O" for ISO format
QDateTime qdt = QDateTime::fromString(str, Qt::ISODate);
See QDateTime::fromString
and DateTime::ToString
docs

Gwen
- 1,436
- 3
- 23
- 31
0
QDateTime::fromString(QString::fromStdWString(msclr::interop::marshal_as<std::wstring>(systemDateTime.ToString("ddMMyyyy HH:mm:ss"))), "ddMMyyyy HH:mm:ss"))

Ayoub
- 361
- 4
- 15