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

Elvis Stansvik elvstone at gmail.com
Thu Jul 14 14:31:24 CEST 2016


2016-07-14 11:13 GMT+02:00 Kristoffersen, Even (NO14)
<Even.Kristoffersen at honeywell.com>:
> 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.

I don't have Qt 5.6 handy here, but I can't reproduce with neither 5.5 nor 5.7:

estan at newton:~$ cat test.qml
import QtQuick 2.0

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

On 5.5:

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

On 5.7:

estan at newton:~$ LC_ALL="en_GB.UTF-8" qmlscene test.qml
qml: 02:24:33 PM
estan at newton:~$ LC_ALL="sv_SE.UTF-8" qmlscene test.qml
qml: 02:24:42 EM
estan at 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

>
> 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
> www.hls-nordic.com
>
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest



More information about the Interest mailing list