Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Creating QDateTime from string

Creating QDateTime from string

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 3 Posters 4.9k Views 1 Watching
  • 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.
  • SPlattenS Offline
    SPlattenS Offline
    SPlatten
    wrote on last edited by SPlatten
    #1

    I am trying to create an instance of QDateTime from a string, the string I have is in the format:

    yyyy/MM/dd hh:mm:ss.zzz
    

    I've tried:

    QDateTime dtmCreated(QDateTime::fromString(strValue));
    

    Where strValue contains:

    2021-08-20T06:45:18
    

    I then output dtmCreated to the console:

    qDebug() << strValue << dtmCreated.isValid();
    

    isValid returns false, I've also tried:

    QDateTime dtmCreated(QDateTime::fromString(strValue, "yyyy/MM/ddThh:mm:ss"));
    

    Same result isValid is false.

    Also tried:

    QDateTime dtmCreated(QDateTime::fromString(strValue, "yyyy-MM-dd HH:mm:ss"));
    

    Same result isValid is false.

    Kind Regards,
    Sy

    J.HilkJ 1 Reply Last reply
    0
    • SPlattenS SPlatten

      @J-Hilk , edited post having tried something else, still not working.

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #4

      @SPlatten
      T

      int main(int argc, char *argv[])
      {
          QDateTime td = QDateTime::fromString("2021-08-20T06:45:18", "yyyy-MM-ddThh:mm:ss");
          qDebug() << td.isValid() << td;
          return 0;
      }
      

      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      SPlattenS 1 Reply Last reply
      0
      • SPlattenS SPlatten

        I am trying to create an instance of QDateTime from a string, the string I have is in the format:

        yyyy/MM/dd hh:mm:ss.zzz
        

        I've tried:

        QDateTime dtmCreated(QDateTime::fromString(strValue));
        

        Where strValue contains:

        2021-08-20T06:45:18
        

        I then output dtmCreated to the console:

        qDebug() << strValue << dtmCreated.isValid();
        

        isValid returns false, I've also tried:

        QDateTime dtmCreated(QDateTime::fromString(strValue, "yyyy/MM/ddThh:mm:ss"));
        

        Same result isValid is false.

        Also tried:

        QDateTime dtmCreated(QDateTime::fromString(strValue, "yyyy-MM-dd HH:mm:ss"));
        

        Same result isValid is false.

        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by
        #2

        @SPlatten said in Creating QDateTime from string:
        So, your date string contains -as separators and your format is passed as /

        2021-08-20T06:45:18

        Do you see the discrepancy? 😉


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        SPlattenS 1 Reply Last reply
        0
        • J.HilkJ J.Hilk

          @SPlatten said in Creating QDateTime from string:
          So, your date string contains -as separators and your format is passed as /

          2021-08-20T06:45:18

          Do you see the discrepancy? 😉

          SPlattenS Offline
          SPlattenS Offline
          SPlatten
          wrote on last edited by
          #3

          @J-Hilk , edited post having tried something else, still not working.

          Kind Regards,
          Sy

          J.HilkJ 1 Reply Last reply
          0
          • SPlattenS SPlatten

            @J-Hilk , edited post having tried something else, still not working.

            J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by
            #4

            @SPlatten
            T

            int main(int argc, char *argv[])
            {
                QDateTime td = QDateTime::fromString("2021-08-20T06:45:18", "yyyy-MM-ddThh:mm:ss");
                qDebug() << td.isValid() << td;
                return 0;
            }
            

            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            SPlattenS 1 Reply Last reply
            0
            • J.HilkJ J.Hilk

              @SPlatten
              T

              int main(int argc, char *argv[])
              {
                  QDateTime td = QDateTime::fromString("2021-08-20T06:45:18", "yyyy-MM-ddThh:mm:ss");
                  qDebug() << td.isValid() << td;
                  return 0;
              }
              
              SPlattenS Offline
              SPlattenS Offline
              SPlatten
              wrote on last edited by
              #5

              @J-Hilk Using "yyyy-MM-ddTHH:mm:ss" works, something minor, in the database using a length of 6 I store the date/timestamp to microsecond resolution, when converting to QDateTime, its only accurate to seconds.

              Kind Regards,
              Sy

              J.HilkJ KroMignonK 2 Replies Last reply
              1
              • SPlattenS SPlatten

                @J-Hilk Using "yyyy-MM-ddTHH:mm:ss" works, something minor, in the database using a length of 6 I store the date/timestamp to microsecond resolution, when converting to QDateTime, its only accurate to seconds.

                J.HilkJ Offline
                J.HilkJ Offline
                J.Hilk
                Moderators
                wrote on last edited by
                #6

                @SPlatten QDateTime should be able to handle milliseconds, because thats what QTime can handle, but nothing further.


                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                1 Reply Last reply
                0
                • SPlattenS SPlatten

                  @J-Hilk Using "yyyy-MM-ddTHH:mm:ss" works, something minor, in the database using a length of 6 I store the date/timestamp to microsecond resolution, when converting to QDateTime, its only accurate to seconds.

                  KroMignonK Offline
                  KroMignonK Offline
                  KroMignon
                  wrote on last edited by
                  #7

                  @SPlatten said in Creating QDateTime from string:

                  when converting to QDateTime, its only accurate to seconds.

                  QDateTime accuracy is down to milliseconds:

                  // Read milliseconds time stamp
                  QDateTime td = QDateTime::fromString("2021-08-20T06:45:18.234", "yyyy-MM-ddThh:mm:ss.zzz");
                  
                  // add milliseconds to QDateTime
                  dt = dt.addMSecs(998);
                  

                  It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                  SPlattenS 1 Reply Last reply
                  0
                  • KroMignonK KroMignon

                    @SPlatten said in Creating QDateTime from string:

                    when converting to QDateTime, its only accurate to seconds.

                    QDateTime accuracy is down to milliseconds:

                    // Read milliseconds time stamp
                    QDateTime td = QDateTime::fromString("2021-08-20T06:45:18.234", "yyyy-MM-ddThh:mm:ss.zzz");
                    
                    // add milliseconds to QDateTime
                    dt = dt.addMSecs(998);
                    
                    SPlattenS Offline
                    SPlattenS Offline
                    SPlatten
                    wrote on last edited by
                    #8

                    @KroMignon , it seems that although the database field is accurate to microseconds, when I read the data into the application the QSqlField:

                    QString strName(crField.name()), strValue(crField.value().toString());
                    

                    crField is a constant reference passed in parameter of QSqlField. strValue when converting the TIMESTAMP(6) from the database which in the database contains 2021-08-20 06:46:18.832407 gets converted to 2021-08-20T06:46:18, this doesn't even contain milliseconds.

                    Kind Regards,
                    Sy

                    KroMignonK 1 Reply Last reply
                    0
                    • SPlattenS SPlatten

                      @KroMignon , it seems that although the database field is accurate to microseconds, when I read the data into the application the QSqlField:

                      QString strName(crField.name()), strValue(crField.value().toString());
                      

                      crField is a constant reference passed in parameter of QSqlField. strValue when converting the TIMESTAMP(6) from the database which in the database contains 2021-08-20 06:46:18.832407 gets converted to 2021-08-20T06:46:18, this doesn't even contain milliseconds.

                      KroMignonK Offline
                      KroMignonK Offline
                      KroMignon
                      wrote on last edited by KroMignon
                      #9

                      @SPlatten said in Creating QDateTime from string:

                      when converting the TIMESTAMP(6) from the database

                      According to QVariant documentation, QVariant::toDateTime() will useQt::ISODate and not Qt::ISODateWithMs. So with the default conversion, you will never got milliseconds.

                      Do you really need micro-seconds in DB? Is TIMESTAMP(3) (milliseconds) not enough for you?
                      With this type, you should be able to parse the DATETIME with milliseconds (with Qt::ISODateWithMs / yyyy-MM-ddThh:mm:ss.zzz).

                      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                      SPlattenS 1 Reply Last reply
                      0
                      • KroMignonK KroMignon

                        @SPlatten said in Creating QDateTime from string:

                        when converting the TIMESTAMP(6) from the database

                        According to QVariant documentation, QVariant::toDateTime() will useQt::ISODate and not Qt::ISODateWithMs. So with the default conversion, you will never got milliseconds.

                        Do you really need micro-seconds in DB? Is TIMESTAMP(3) (milliseconds) not enough for you?
                        With this type, you should be able to parse the DATETIME with milliseconds (with Qt::ISODateWithMs / yyyy-MM-ddThh:mm:ss.zzz).

                        SPlattenS Offline
                        SPlattenS Offline
                        SPlatten
                        wrote on last edited by
                        #10

                        @KroMignon , to be honest milliseconds is probably good enough, but the higher resolution the better in the database, from your post is there a way to get milliseconds is the conversion doesn't use Qt::ISODateWithMS ?

                        Kind Regards,
                        Sy

                        KroMignonK 1 Reply Last reply
                        0
                        • SPlattenS SPlatten

                          @KroMignon , to be honest milliseconds is probably good enough, but the higher resolution the better in the database, from your post is there a way to get milliseconds is the conversion doesn't use Qt::ISODateWithMS ?

                          KroMignonK Offline
                          KroMignonK Offline
                          KroMignon
                          wrote on last edited by
                          #11

                          @SPlatten said in Creating QDateTime from string:

                          to be honest milliseconds is probably good enough, but the higher resolution the better in the database, from your post is there a way to get milliseconds is the conversion doesn't use Qt::ISODateWithMS ?

                          Yes, I think so:

                          QDateTime dt = QDateTime::fromString(crField.value().toString(), Qt::ISODateWithMS);
                          

                          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                          SPlattenS 1 Reply Last reply
                          0
                          • KroMignonK KroMignon

                            @SPlatten said in Creating QDateTime from string:

                            to be honest milliseconds is probably good enough, but the higher resolution the better in the database, from your post is there a way to get milliseconds is the conversion doesn't use Qt::ISODateWithMS ?

                            Yes, I think so:

                            QDateTime dt = QDateTime::fromString(crField.value().toString(), Qt::ISODateWithMS);
                            
                            SPlattenS Offline
                            SPlattenS Offline
                            SPlatten
                            wrote on last edited by
                            #12

                            @KroMignon, I'm not sure that is going to work because the toString function already removes the fraction part of the time.

                            Kind Regards,
                            Sy

                            1 Reply Last reply
                            0

                            • Login

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