Discussion:
[Interest] [QML] Qt.formatDateTime() and AM/PM
Kristoffersen, Even (NO14)
2016-07-14 09:13:56 UTC
Permalink
Hello,

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
***@honeywell.com
www.hls-nordic.com
Elvis Stansvik
2016-07-14 12:31:24 UTC
Permalink
2016-07-14 11:13 GMT+02:00 Kristoffersen, Even (NO14)
Post by Kristoffersen, Even (NO14)
Hello,
I'm having some issue with trying to display a 12 hour clock with am/pm suffix.
"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.
I don't have Qt 5.6 handy here, but I can't reproduce with neither 5.5 nor 5.7:

***@newton:~$ cat test.qml
import QtQuick 2.0

Item {
Component.onCompleted: console.info(Qt.formatDateTime(new Date(),
"hh:mm:ss AP"));
}
***@newton:~$

On 5.5:

***@newton:~$ LC_ALL="en_GB.UTF-8" qmlscene test.qml
qml: 02:24:33 P.M.
***@newton:~$ LC_ALL="sv_SE.UTF-8" qmlscene test.qml
qml: 02:24:42 EM
***@newton:~$

On 5.7:

***@newton:~$ LC_ALL="en_GB.UTF-8" qmlscene test.qml
qml: 02:24:33 PM
***@newton:~$ LC_ALL="sv_SE.UTF-8" qmlscene test.qml
qml: 02:24:42 EM
***@newton:~$

Note the difference in the en_GB.UTF-8 output on 5.5 vs 5.7 though...
(P.M. vs PM).

I also think it's a little dubious to have the AM/PM localized in the
Swedish locale. I'm a swede and I've very rarely seen "EM" / "FM"
used. I guess it stands for eftermiddag / förmiddag (afternoon /
before noon).

Not sure why it's not working for you on 5.6 though, sorry :/

Elvis
Post by Kristoffersen, Even (NO14)
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
www.hls-nordic.com
_______________________________________________
Interest mailing list
http://lists.qt-project.org/mailman/listinfo/interest
Rainer Wiesenfarth
2016-07-14 13:11:47 UTC
Permalink
Post by Elvis Stansvik
import QtQuick 2.0
Item {
Component.onCompleted: console.info(Qt.formatDateTime(new Date(),
"hh:mm:ss AP"));
}
​I am able to reproduce the problem with 5.6.0 on Windows, using your
snippet:​

​D:\Develop\tests>qmlscene test.qml
qml: Do 03:04:31
​
​No am/pm is added, but hour is 0-12. But...

... this is with "Region and Language" set to German. If I set it to
English, the output is as expected:

D:\Develop\tests>qmlscene test.qml
qml: Thu 03:05:37 PM

​So the problem seems to be the missing translation for "PM" and "AM"​, but
this may be related to Windows rather than Qt.
--
Software Engineer | Trimble Imaging Division
RotebÃŒhlstraße 81 | 70178 Stuttgart | Germany
Office +49 711 22881 0 | Fax +49 711 22881 11
http://www.trimble.com/imaging/ | http://www.inpho.de/

Trimble Germany GmbH, Am Prime Parc 11, 65479 Raunheim
Eingetragen beim Amtsgericht Darmstadt unter HRB 83893,
GeschÀftsfÌhrer: Dr. Frank Heimberg, JÌrgen Kesper
Kristoffersen, Even (NO14)
2016-07-14 13:21:27 UTC
Permalink
Elvis: Tack
Rainer: Danke

Maybe the format function uses the system locale instead of default QLocale, that would explain it.


-Even


From: Interest [mailto:interest-bounces+even.kristoffersen=***@qt-project.org] On Behalf Of Rainer Wiesenfarth
Sent: 14. juli 2016 15:12
To: interestqt-project.org <***@qt-project.org>
Subject: Re: [Interest] [QML] Qt.formatDateTime() and AM/PM


2016-07-14 14:31 GMT+02:00 Elvis Stansvik <***@gmail.com<mailto:***@gmail.com>>:
***@newton:~$ cat test.qml
import QtQuick 2.0

Item {
Component.onCompleted: console.info<http://console.info>(Qt.formatDateTime(new Date(),
"hh:mm:ss AP"));
}
***@newton:~$

​I am able to reproduce the problem with 5.6.0 on Windows, using your snippet:​

​D:\Develop\tests>qmlscene test.qml
qml: Do 03:04:31
​
​No am/pm is added, but hour is 0-12. But...

... this is with "Region and Language" set to German. If I set it to English, the output is as expected:

D:\Develop\tests>qmlscene test.qml
qml: Thu 03:05:37 PM

​So the problem seems to be the missing translation for "PM" and "AM"​, but this may be related to Windows rather than Qt.
--
Software Engineer | Trimble Imaging Division
RotebÃŒhlstraße 81 | 70178 Stuttgart | Germany
Office +49 711 22881 0 | Fax +49 711 22881 11
http://www.trimble.com/imaging/ | http://www.inpho.de/

Trimble Germany GmbH, Am Prime Parc 11, 65479 Raunheim
Eingetragen beim Amtsgericht Darmstadt unter HRB 83893,
GeschÀftsfÌhrer: Dr. Frank Heimberg, JÌrgen Kesper
Loading...