Lets play a game... thermonuclear wa...sorry QDate to JS Date conversion!
-
So if I create a Date in QML JS. Say new Date(). Then I send this Date to a function that accepts QDate then it will have the correct date stored inside. However, if I take the same QDate and convert it to Date by returning a QDate in QML JS it will be off by one day.
I store a date in my database by converting the QDate to a string in format "yyyy/MM/dd". I can convert back to QDate easy using same format string. Then I see the JS Date loses a day when QDate to Date occurs.
So I have this terrible hack when I first read in this Date from database:
tdate.setDate(tdate.getDate()+1)Don't use this approach. Use QDateTime instead of QDate.I will submit a bug report to Qt, but I wanted to share my fix in case other people run into this. Kind of in shock this is a bug at all.
Qt 5.15.8 x86
-
So if I create a Date in QML JS. Say new Date(). Then I send this Date to a function that accepts QDate then it will have the correct date stored inside. However, if I take the same QDate and convert it to Date by returning a QDate in QML JS it will be off by one day.
I store a date in my database by converting the QDate to a string in format "yyyy/MM/dd". I can convert back to QDate easy using same format string. Then I see the JS Date loses a day when QDate to Date occurs.
So I have this terrible hack when I first read in this Date from database:
tdate.setDate(tdate.getDate()+1)Don't use this approach. Use QDateTime instead of QDate.I will submit a bug report to Qt, but I wanted to share my fix in case other people run into this. Kind of in shock this is a bug at all.
Qt 5.15.8 x86
@Demolishun said in Lets play a game... thermonuclear wa...sorry QDate to JS Date conversion!:
Qt 5.15.8 x86
Which compiler?
I know of at least 1 case where JS Date issues are caused by an issue in the compiler: https://bugreports.qt.io/browse/QTBUG-95993
-
Repeatable gcc 7.5.0 Qt 5.15.2 x86 :
main.qml:
import QtQuick 2.15 import QtQuick.Window 2.15 Window { width: 640 height: 480 visible: true title: qsTr("QDate to Date bug") Component.onCompleted: { let date = new Date() console.log(date) dobj.setDate(date) let ndate = dobj.date() console.log(ndate) } }
main.cpp:
#include <QGuiApplication> #include <QQmlApplicationEngine> #include <QQmlContext> #include <QDate> class DateObject : public QObject { Q_OBJECT public: DateObject(QObject* parent=nullptr) : QObject(parent) { } QDate m_date; Q_INVOKABLE QDate date(){ qDebug() << "date()" << m_date; return m_date; } Q_INVOKABLE void setDate(QDate ndate){ qDebug() << "setDate()" << ndate; m_date = ndate; } }; int main(int argc, char *argv[]) { #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); #endif QGuiApplication app(argc, argv); QQmlApplicationEngine engine; auto context = engine.rootContext(); DateObject dobj; context->setContextProperty("dobj", &dobj); const QUrl url(QStringLiteral("qrc:/main.qml")); QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, &app, [url](QObject *obj, const QUrl &objUrl) { if (!obj && url == objUrl) QCoreApplication::exit(-1); }, Qt::QueuedConnection); engine.load(url); return app.exec(); } #include "main.moc"
result:
qml: Mon Feb 21 08:33:22 2022 GMT-0700 setDate() QDate("2022-02-21") date() QDate("2022-02-21") qml: Sun Feb 20 17:00:00 2022 GMT-0700
-
My approach was lossy. QDate loses time information. Changing data type for C++ to QDateTime keeps the time info even if I don't need that. The time zones have an affect. It seems JS Date can interchange between C++ QDate or QDateTime. QDateTime is the preferred and doesn't cause errors.