Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Lets play a game... thermonuclear wa...sorry QDate to JS Date conversion!
QtWS25 Last Chance

Lets play a game... thermonuclear wa...sorry QDate to JS Date conversion!

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 Posts 3 Posters 395 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Demolishun
    wrote on 21 Feb 2022, 04:18 last edited by Demolishun
    #1

    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

    J 1 Reply Last reply 21 Feb 2022, 05:29
    0
    • D Demolishun
      21 Feb 2022, 04:18

      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

      J Offline
      J Offline
      JKSH
      Moderators
      wrote on 21 Feb 2022, 05:29 last edited by JKSH
      #2

      @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

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      1 Reply Last reply
      0
      • F Offline
        F Offline
        fcarney
        wrote on 21 Feb 2022, 15:47 last edited by fcarney
        #3

        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
        

        C++ is a perfectly valid school of magic.

        1 Reply Last reply
        0
        • D Offline
          D Offline
          Demolishun
          wrote on 22 Feb 2022, 00:52 last edited by
          #4

          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.

          1 Reply Last reply
          1

          4/4

          22 Feb 2022, 00:52

          • Login

          • Login or register to search.
          4 out of 4
          • First post
            4/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved