QDateTime::currentDateTime() sometimes return ???
-
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 ???
-
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 ???
-
So, I find out that, for example if the windows language is Egypt(arabic) so the time & date is displayed not in english numbers. It is in Egypt(arabic) writing. Is there a function to get (format) time & date in English language numbers?
@asmant
That's what QLocale is for :) -
@asmant
That's what QLocale is for :) -
@J-Hilk I have set locale to English in the main.cpp. Do I have to do it somewhere else?
QLocale locale = QLocale(QLocale::English); QLocale::setDefault(locale);
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! -
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!