2

So I'm sending a QDateTime to QML in order to display it and do some checkings (ideally I would like to be able to use all javascript date functions like getTime(),getMonth()...), and I realized if I send:

QDateTime(2019-10-30 11:15:00.000 CET Qt::TimeZone Europe/Amsterdam )

And then I read the date on QML, I get the time on the local timezone instead of the one set on the QDateTime...

Wed Oct 30 06:15:00 2019 GMT-0400 (NY timezone)

Is there any way I can keep the timezone in the QML side? Thanks!!

laurapons
  • 971
  • 13
  • 32
  • How are you displaying the date/time value on QML side? Or IOW, how do you know the timezone is wrong? I'm wondering if it's just converting to local time by default for display. eg. default for `Qt.formatDateTime()` is local time. Although I don't understand the 15min difference in any case. – Maxim Paperno Oct 21 '19 at 17:58
  • console.log(date) prints out "Wed Oct 30 06:15:00 2019 GMT-0400". If I want to compare hours or do some checkings with only the time with a custom timezone different from the local one, then it's not that straight forward... The 15min difference was my fault, I'll edit it sorry – laurapons Oct 21 '19 at 18:03
  • Seems to be the case that the time zone info is lost "in translation" to QML. I tried it by setting a `contextProperty()` with a `QDateTime` like in your example, but it probably doesn't matter how it's passed in. The [docs](https://doc.qt.io/qt-5/qml-date.html) are rather confusing on this subject as well. :( – Maxim Paperno Oct 21 '19 at 18:40

2 Answers2

0

The javascript Date class is quite limited. It holds an internal UTC time/date and doesn't allow to change the timezone. It also displays by default converting to the local timezone, which is what you have observed.

There is a javascript Moment library with timezone support, to alleviate some of the Date shortcomings, but it is not fully compatible with QML, IIRC.

Anyway, the best route seems to be avoiding Date objects in QML as much as you can, and use something else instead. Either a javascript alternative, or even better, your own C++ class encapsulating QDateTime, exposed as a context property to QML, with whatever methods you need in the QML side. Something like this example ApplicationData from the official Qt documentation.

Former contributor
  • 2,466
  • 2
  • 10
  • 15
  • ok, I'll try creating my own class to support the functions I need from Date... or do all the logic in qt side and send values as strings... thanks! – laurapons Oct 22 '19 at 18:46
0

Following the @Pedro's advice (I don't know how to tag you properly...), I used moment.js as well as moment-timezone.js so I could reference any date to the timezone I want. To anyone interested, that's how I did it:

import QtQuick 2.9
import "./moment.js" as Moment
import "./moment-timezone-with-data.js" as MomentTimezone

Item {
    anchors.fill: parent
    property date customDate: new Date(); // local timezone is "America/New_York"

    function getDateWithTimeZone(date, timeZone) {
        var momentDate = moment(new Date(date));
        var momentDateTz = momentDate.tz(timeZone);
        return momentDateTz;
    }

    Text {
        anchors.centerIn: parent
        text: customDate.toLocaleString() + "\n"
              + getDateWithTimeZone(customDate, "Europe/Paris").toString()
    }
}

which gives the following output:

enter image description here

laurapons
  • 971
  • 13
  • 32