[Interest] [QML] Qt.formatDateTime() and AM/PM

Kristoffersen, Even (NO14) Even.Kristoffersen at Honeywell.com
Thu Jul 14 11:13:56 CEST 2016


I'm having some issue with trying to display a 12 hour clock with am/pm suffix.

Documentation (http://doc.qt.io/qt-5/qml-qtqml-qt.html#formatDateTime-method) says:
"AP	use AM/PM display. AP will be replaced by either "AM" or "PM"."

However neither of the following work as described with Qt 5.6.
clock.text = Qt.formatDateTime(new Date(), "hh:mm:ss AP"); //14:01:03 displayed as "02:01:03"
clock.text = Qt.formatTime(new Date(), "hh:mm:ss AP"); //Same as above

They display the time in 12h format, but the suffix is not there.

Locale has been to English/UK from C++ side of the application:
QLocale::setDefault(QLocale(QLocale::English, QLocale::UnitedKingdom));

Anyone has any pointers as to why this doesn't work?

I have currently solved it with the code below, but it would be nice to solve it with one line of code instead.

var time = new Date();

//Update time depending on 12/24h format
if (Qt.locale().amText !== "") {
    if (time.getHours() > 11) {
        clock.text = Qt.formatTime(time, "hh:mm:ss AP") + Qt.locale().pmText;
    } else {
        clock.text = Qt.formatTime(time, "hh:mm:ss AP") + Qt.locale().amText;
} else {
    clock.text = Qt.formatTime(time, "hh:mm:ss");

Best regards,

Even Kristoffersen
Software Developer
Honeywell | Security and Fire 
T: +47 32 24 48 00
D: +47 32 24 48 23
M: +47 95 22 37 53
even.kristoffersen at honeywell.com

More information about the Interest mailing list