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
Forum Updated to NodeBB v4.3 + New Features

Creating QDateTime from string

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 3 Posters 3.7k 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.
  • S Offline
    S Offline
    SPlatten
    wrote on 23 Aug 2021, 10:28 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 1 Reply Last reply 23 Aug 2021, 10:35
    0
    • S SPlatten
      23 Aug 2021, 10:36

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

      J Online
      J Online
      J.Hilk
      Moderators
      wrote on 23 Aug 2021, 10:38 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.

      S 1 Reply Last reply 23 Aug 2021, 10:39
      0
      • S SPlatten
        23 Aug 2021, 10:28

        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 Online
        J Online
        J.Hilk
        Moderators
        wrote on 23 Aug 2021, 10:35 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.

        S 1 Reply Last reply 23 Aug 2021, 10:36
        0
        • J J.Hilk
          23 Aug 2021, 10:35

          @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? 😉

          S Offline
          S Offline
          SPlatten
          wrote on 23 Aug 2021, 10:36 last edited by
          #3

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

          Kind Regards,
          Sy

          J 1 Reply Last reply 23 Aug 2021, 10:38
          0
          • S SPlatten
            23 Aug 2021, 10:36

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

            J Online
            J Online
            J.Hilk
            Moderators
            wrote on 23 Aug 2021, 10:38 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.

            S 1 Reply Last reply 23 Aug 2021, 10:39
            0
            • J J.Hilk
              23 Aug 2021, 10:38

              @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;
              }
              
              S Offline
              S Offline
              SPlatten
              wrote on 23 Aug 2021, 10:39 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 KroMignonK 2 Replies Last reply 23 Aug 2021, 10:53
              1
              • S SPlatten
                23 Aug 2021, 10:39

                @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 Online
                J Online
                J.Hilk
                Moderators
                wrote on 23 Aug 2021, 10:53 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
                • S SPlatten
                  23 Aug 2021, 10:39

                  @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 23 Aug 2021, 11:26 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)

                  S 1 Reply Last reply 23 Aug 2021, 11:40
                  0
                  • KroMignonK KroMignon
                    23 Aug 2021, 11:26

                    @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);
                    
                    S Offline
                    S Offline
                    SPlatten
                    wrote on 23 Aug 2021, 11:40 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 23 Aug 2021, 12:04
                    0
                    • S SPlatten
                      23 Aug 2021, 11:40

                      @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 23 Aug 2021, 12:04 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)

                      S 1 Reply Last reply 23 Aug 2021, 12:09
                      0
                      • KroMignonK KroMignon
                        23 Aug 2021, 12:04

                        @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).

                        S Offline
                        S Offline
                        SPlatten
                        wrote on 23 Aug 2021, 12:09 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 23 Aug 2021, 12:18
                        0
                        • S SPlatten
                          23 Aug 2021, 12:09

                          @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 23 Aug 2021, 12:18 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)

                          S 1 Reply Last reply 23 Aug 2021, 12:23
                          0
                          • KroMignonK KroMignon
                            23 Aug 2021, 12:18

                            @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);
                            
                            S Offline
                            S Offline
                            SPlatten
                            wrote on 23 Aug 2021, 12:23 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

                            1/12

                            23 Aug 2021, 10:28

                            • Login

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