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. Why this date.fromstring is not working ?
Forum Updated to NodeBB v4.3 + New Features

Why this date.fromstring is not working ?

Scheduled Pinned Locked Moved Unsolved General and Desktop
20 Posts 5 Posters 1.8k 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.
  • R RahibeMeryem

    @SGaist said in Why this date.fromstring is not working ?:

    DATE = QDate::fromString("17-19-47 2019-12-10" , "hh-mm-ss yyyy-MM-dd");

            QDate DATE2 = QDate::fromString("17-19-47 2019-12-10" , "hh-mm-ss yyyy-MM-dd");        
    
    

    Still gives Invalid error DATE2 Inalid

    .. What am I missing ?

    JonBJ Offline
    JonBJ Offline
    JonB
    wrote on last edited by JonB
    #7

    @RahibeMeryem
    I don't know whether what @jsulm has just written is correct or not, but https://doc.qt.io/qt-5/qdate.html#fromString-1 says it only accepts date format specifiers, you have date and time. Don't you need QDateTime::fromString(), as in https://doc.qt.io/qt-5/qdatetime.html#fromString-1 ? Then you may want the HH instead of hh (read the doc page for the distinction).

    jsulmJ 2 Replies Last reply
    0
    • JonBJ JonB

      @RahibeMeryem
      I don't know whether what @jsulm has just written is correct or not, but https://doc.qt.io/qt-5/qdate.html#fromString-1 says it only accepts date format specifiers, you have date and time. Don't you need QDateTime::fromString(), as in https://doc.qt.io/qt-5/qdatetime.html#fromString-1 ? Then you may want the HH instead of hh (read the doc page for the distinction).

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #8

      @JonB said in Why this date.fromstring is not working ?:

      what @jsulm has just written is correct or not

      It is - just tested.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      JonBJ 2 Replies Last reply
      1
      • jsulmJ jsulm

        @JonB said in Why this date.fromstring is not working ?:

        what @jsulm has just written is correct or not

        It is - just tested.

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #9

        @jsulm
        You tested it with QDate instead of QDateTime, when the OP is passing a date+time value and date+time format? He wants to pass in a date+time with all the formatting for time too, yet he only wants a date returned?

        1 Reply Last reply
        0
        • JonBJ JonB

          @RahibeMeryem
          I don't know whether what @jsulm has just written is correct or not, but https://doc.qt.io/qt-5/qdate.html#fromString-1 says it only accepts date format specifiers, you have date and time. Don't you need QDateTime::fromString(), as in https://doc.qt.io/qt-5/qdatetime.html#fromString-1 ? Then you may want the HH instead of hh (read the doc page for the distinction).

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #10

          @JonB @RahibeMeryem Yes, with QDateTime it works with hh also.
          Sometimes it's so easy :-)

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          JonBJ 1 Reply Last reply
          0
          • jsulmJ jsulm

            @JonB @RahibeMeryem Yes, with QDateTime it works with hh also.
            Sometimes it's so easy :-)

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #11

            @jsulm
            My point being: if you really only want a QDate back, why would you include the time specifiers in the format string? I'm wondering whether the user intends QDateTime::fromString()...?

            jsulmJ 1 Reply Last reply
            0
            • JonBJ JonB

              @jsulm
              My point being: if you really only want a QDate back, why would you include the time specifiers in the format string? I'm wondering whether the user intends QDateTime::fromString()...?

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #12

              @JonB said in Why this date.fromstring is not working ?:

              My point being: if you really only want a QDate back, why would you include the time specifiers in the format string?

              You should ask OP, not me

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              JonBJ 1 Reply Last reply
              0
              • jsulmJ jsulm

                @JonB said in Why this date.fromstring is not working ?:

                My point being: if you really only want a QDate back, why would you include the time specifiers in the format string?

                You should ask OP, not me

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #13

                @jsulm
                My use of "you" is the "communal" "you", not the "personal" "you" aimed at you :)

                1 Reply Last reply
                0
                • jsulmJ jsulm

                  @JonB said in Why this date.fromstring is not working ?:

                  what @jsulm has just written is correct or not

                  It is - just tested.

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #14

                  @jsulm , @RahibeMeryem
                  I'm sorry, but it is not the HH vs hh (don't know what you tested for that @jsulm), it is indeed as I said that you need QDateTime for your format:

                  >>> from PySide2 import QtCore
                  >>> QtCore.QDate.fromString("17-19-47 2019-12-10" , "hh-mm-ss yyyy-MM-dd")
                  PySide2.QtCore.QDate(0, 0, 0)
                  >>> QtCore.QDateTime.fromString("17-19-47 2019-12-10" , "hh-mm-ss yyyy-MM-dd")
                  PySide2.QtCore.QDateTime(2019, 12, 10, 17, 19, 47, 0, 0)
                  >>> QtCore.QDate.fromString("17-19-47 2019-12-10" , "HH-mm-ss yyyy-MM-dd")
                  PySide2.QtCore.QDate(0, 0, 0)
                  >>> QtCore.QDateTime.fromString("17-19-47 2019-12-10" , "HH-mm-ss yyyy-MM-dd")
                  PySide2.QtCore.QDateTime(2019, 12, 10, 17, 19, 47, 0, 0)
                  >>> 
                  
                  

                  The reason being: if you use QDate, the leading "HH-mm-ss" or whatever will not match against the time, instead it will be treated as literal and therefore the input will fail to match, whatever you do....

                  jsulmJ 1 Reply Last reply
                  0
                  • JonBJ JonB

                    @jsulm , @RahibeMeryem
                    I'm sorry, but it is not the HH vs hh (don't know what you tested for that @jsulm), it is indeed as I said that you need QDateTime for your format:

                    >>> from PySide2 import QtCore
                    >>> QtCore.QDate.fromString("17-19-47 2019-12-10" , "hh-mm-ss yyyy-MM-dd")
                    PySide2.QtCore.QDate(0, 0, 0)
                    >>> QtCore.QDateTime.fromString("17-19-47 2019-12-10" , "hh-mm-ss yyyy-MM-dd")
                    PySide2.QtCore.QDateTime(2019, 12, 10, 17, 19, 47, 0, 0)
                    >>> QtCore.QDate.fromString("17-19-47 2019-12-10" , "HH-mm-ss yyyy-MM-dd")
                    PySide2.QtCore.QDate(0, 0, 0)
                    >>> QtCore.QDateTime.fromString("17-19-47 2019-12-10" , "HH-mm-ss yyyy-MM-dd")
                    PySide2.QtCore.QDateTime(2019, 12, 10, 17, 19, 47, 0, 0)
                    >>> 
                    
                    

                    The reason being: if you use QDate, the leading "HH-mm-ss" or whatever will not match against the time, instead it will be treated as literal and therefore the input will fail to match, whatever you do....

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by jsulm
                    #15

                    @JonB That's why I wrote "Yes, with QDateTime it works with hh also.".
                    With QDate it works with HH, but not hh - I tested that:

                    QDate DATE2 = QDate::fromString("17-19-47 2019-12-10" , "HH-mm-ss yyyy-MM-dd");
                    

                    And yes, it is correct that one should use QDateTime instead of QDate if there is time and date.

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    JonBJ 1 Reply Last reply
                    0
                    • jsulmJ jsulm

                      @JonB That's why I wrote "Yes, with QDateTime it works with hh also.".
                      With QDate it works with HH, but not hh - I tested that:

                      QDate DATE2 = QDate::fromString("17-19-47 2019-12-10" , "HH-mm-ss yyyy-MM-dd");
                      

                      And yes, it is correct that one should use QDateTime instead of QDate if there is time and date.

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by JonB
                      #16

                      @jsulm
                      If it works for you with HH plus QDate, could you please explain the output I showed above where it does not work? Thanks.

                      >>> QtCore.QDate.fromString("17-19-47 2019-12-10" , "HH-mm-ss yyyy-MM-dd")
                      PySide2.QtCore.QDate(0, 0, 0)
                      

                      which is an invalid QDate. I am Qt 5.12.2, Ubuntu 19.04.

                      jsulmJ 1 Reply Last reply
                      0
                      • J.HilkJ Online
                        J.HilkJ Online
                        J.Hilk
                        Moderators
                        wrote on last edited by
                        #17

                        From my point of view, your both wrong @JonB & @jsulm πŸ˜‰

                        the docu only states https://doc.qt.io/qt-5/qdate.html#fromString-1

                        that only yand Mand dare valid formats everything else is interpreted as a literal character.

                        and my test confirms it:

                        int main(int argc, char *argv[])
                        {
                            QApplication a(argc, argv);
                        
                        
                            QDate d1 = QDate::fromString("17-19-47 2019-12-10" , "hh-mm-ss yyyy-MM-dd");
                            QDate d2 = QDate::fromString("17-19-47 2019-12-10" , "HH-mm-ss yyyy-MM-dd");
                        
                            QDate d3 = QDate::fromString("17-19-47 2019-12-10" , "17-19-47 yyyy-MM-dd");
                            QDate d4 = QDateTime::fromString("17-19-47 2019-12-10" , "hh-mm-ss yyyy-MM-dd").date();
                            qDebug() << d1 << d2 << d3 << d4;
                        
                        //    return a.exec();
                        }
                        
                        Result:
                        QDate(Invalid) QDate(Invalid) QDate("2019-12-10") QDate("2019-12-10")
                        

                        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.

                        JonBJ 1 Reply Last reply
                        0
                        • JonBJ JonB

                          @jsulm
                          If it works for you with HH plus QDate, could you please explain the output I showed above where it does not work? Thanks.

                          >>> QtCore.QDate.fromString("17-19-47 2019-12-10" , "HH-mm-ss yyyy-MM-dd")
                          PySide2.QtCore.QDate(0, 0, 0)
                          

                          which is an invalid QDate. I am Qt 5.12.2, Ubuntu 19.04.

                          jsulmJ Offline
                          jsulmJ Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on last edited by
                          #18

                          @JonB OK, correction: with QDate it does not work (not with hh and not with HH). I was thinking it does, because my small test application outputs date/time in another place, so it was looking like it was working. Sorry for confusion :-)

                          https://forum.qt.io/topic/113070/qt-code-of-conduct

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

                            From my point of view, your both wrong @JonB & @jsulm πŸ˜‰

                            the docu only states https://doc.qt.io/qt-5/qdate.html#fromString-1

                            that only yand Mand dare valid formats everything else is interpreted as a literal character.

                            and my test confirms it:

                            int main(int argc, char *argv[])
                            {
                                QApplication a(argc, argv);
                            
                            
                                QDate d1 = QDate::fromString("17-19-47 2019-12-10" , "hh-mm-ss yyyy-MM-dd");
                                QDate d2 = QDate::fromString("17-19-47 2019-12-10" , "HH-mm-ss yyyy-MM-dd");
                            
                                QDate d3 = QDate::fromString("17-19-47 2019-12-10" , "17-19-47 yyyy-MM-dd");
                                QDate d4 = QDateTime::fromString("17-19-47 2019-12-10" , "hh-mm-ss yyyy-MM-dd").date();
                                qDebug() << d1 << d2 << d3 << d4;
                            
                            //    return a.exec();
                            }
                            
                            Result:
                            QDate(Invalid) QDate(Invalid) QDate("2019-12-10") QDate("2019-12-10")
                            
                            JonBJ Offline
                            JonBJ Offline
                            JonB
                            wrote on last edited by JonB
                            #19

                            @J-Hilk said in Why this date.fromstring is not working ?:

                            From my point of view, your both wrong @JonB & @jsulm πŸ˜‰

                            Interesting, because what you have shown is exactly what I have said! :)

                            In the case of working

                            QDate d3 = QDate::fromString("17-19-47 2019-12-10" , "17-19-47 yyyy-MM-dd");
                            

                            that works only because the literal leading 17-19-47 in the format string matches the input without "placeholder substitutions".

                            As for the HH vs hh when using QDateTime, that should be irrelevant for the input shown, as per https://doc.qt.io/qt-5/qdatetime.html#fromString-1

                            hh the hour with a leading zero (00 to 23 or 01 to 12 if AM/PM display)
                            HH the hour with a leading zero (00 to 23, even with AM/PM display)

                            as the only difference is to do with "AM/PM", which the input string does not have.

                            J.HilkJ 1 Reply Last reply
                            0
                            • JonBJ JonB

                              @J-Hilk said in Why this date.fromstring is not working ?:

                              From my point of view, your both wrong @JonB & @jsulm πŸ˜‰

                              Interesting, because what you have shown is exactly what I have said! :)

                              In the case of working

                              QDate d3 = QDate::fromString("17-19-47 2019-12-10" , "17-19-47 yyyy-MM-dd");
                              

                              that works only because the literal leading 17-19-47 in the format string matches the input without "placeholder substitutions".

                              As for the HH vs hh when using QDateTime, that should be irrelevant for the input shown, as per https://doc.qt.io/qt-5/qdatetime.html#fromString-1

                              hh the hour with a leading zero (00 to 23 or 01 to 12 if AM/PM display)
                              HH the hour with a leading zero (00 to 23, even with AM/PM display)

                              as the only difference is to do with "AM/PM", which the input string does not have.

                              J.HilkJ Online
                              J.HilkJ Online
                              J.Hilk
                              Moderators
                              wrote on last edited by
                              #20

                              @JonB πŸ˜†sorry I got the impression you were arguing for the lower case hhwhich is of course wrong in Date::fromString() missed the other post!


                              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
                              1

                              • Login

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