QDateTime::currentDateTime() sometimes return ???
Solved
General and Desktop
-
Hello, in my windows app's logs I get current date-time like this:
QDateTime dateTime = dateTime.currentDateTime(); QString currentDateTime = dateTime.toString("yyyy-MM-dd HHmmss");
It worked well until I noticed that sometimes the time is ????-??-?? ??:??:??. How it can be that way? I know that windows time can be incorrect, but it should return incorrect time and not ???
-
@asmant
That's what QLocale is for :) -
Hello!
@J-Hilk meant to use the
dateTimeFormat
(https://doc.qt.io/qt-5/qlocale.html#dateTimeFormat) function, it will return the date format used for the current locale.Also, you can try out my solution:
QLocale testLocale = QLocale(QLocale::English, QLocale::UnitedStates); QString dateTimeText = testLocale.toString(QDateTime::currentDateTime(), "dddd / dd / MMMM / yyyy / hh:mm:ss AP"); qDebug() << dateTimeText;
It will display date time using
English
locale (United States). You can changeQLocale
and format it to your needs. Happy coding! -
@Cobra91151 That's what I wanted to do. Thank you!