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. QDateTime fromString () does not work
QtWS25 Last Chance

QDateTime fromString () does not work

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 5 Posters 1.7k 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.
  • S Offline
    S Offline
    Samuel Ives
    wrote on last edited by
    #1

    I am trying to get a DATETIME from an sql server and the formatting is being yyyy-MM-dd hh:mm:ss but an invalid date is returned.

    This is the value returned by query 2019-06-14T08:43:48.000, also inserts a timestamp manually as shown below but the value remains invalid

    QDateTime Vendas::feitaEm() const
    {
        QDateTime dt;
        dt.fromString("2019-06-16 09:45:05", "yyyy-MM-dd hh:mm:ss");
        qDebug() << dt.date();
        return QDateTime::fromString(data.value(7), "yyyy-MM-dd hh:mm:ss");
    }
    
    aha_1980A 1 Reply Last reply
    0
    • S Samuel Ives

      I am trying to get a DATETIME from an sql server and the formatting is being yyyy-MM-dd hh:mm:ss but an invalid date is returned.

      This is the value returned by query 2019-06-14T08:43:48.000, also inserts a timestamp manually as shown below but the value remains invalid

      QDateTime Vendas::feitaEm() const
      {
          QDateTime dt;
          dt.fromString("2019-06-16 09:45:05", "yyyy-MM-dd hh:mm:ss");
          qDebug() << dt.date();
          return QDateTime::fromString(data.value(7), "yyyy-MM-dd hh:mm:ss");
      }
      
      aha_1980A Offline
      aha_1980A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Samuel-Ives

      QDateTime dt; dt.fromString("2019-06-16 09:45:05", "yyyy-MM-dd hh:mm:ss"); qDebug() << dt.date();

      Try:

       QDateTime dt = QDateTime::fromString("2019-06-16 09:45:05", "yyyy-MM-dd hh:mm:ss"); 
      qDebug() << dt.date();
      

      Qt has to stay free or it will die.

      1 Reply Last reply
      3
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #3

        @aha_1980 answer is correct but a bit more explanation. QDateTime::fromString is static so i can't change the item you call it from, you have to assign something to it

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        3
        • G Offline
          G Offline
          Gerd
          wrote on last edited by
          #4

          also, for your request the formatstring should be

          dt = QDateTime::fromString("2019-06-14T08:43:48.000", "yyyy-MM-ddThh:mm:ss.zzz");
          
          VRoninV S 2 Replies Last reply
          2
          • G Gerd

            also, for your request the formatstring should be

            dt = QDateTime::fromString("2019-06-14T08:43:48.000", "yyyy-MM-ddThh:mm:ss.zzz");
            
            VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #5

            @Gerd Or, since that's the ISO-8601 representation: QDateTime::fromString("2019-06-14T08:43:48.000", Qt::ISODateWithMs)

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            1 Reply Last reply
            2
            • G Gerd

              also, for your request the formatstring should be

              dt = QDateTime::fromString("2019-06-14T08:43:48.000", "yyyy-MM-ddThh:mm:ss.zzz");
              
              S Offline
              S Offline
              Samuel Ives
              wrote on last edited by
              #6

              @Gerd it worked, but it's strange that the result of the query is with a 'T' in the timestamp while the server is not present

              jsulmJ 1 Reply Last reply
              0
              • S Samuel Ives

                @Gerd it worked, but it's strange that the result of the query is with a 'T' in the timestamp while the server is not present

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

                @Samuel-Ives said in QDateTime fromString () does not work:

                but it's strange that the result of the query is with a 'T' in the timestamp while the server is not present

                Do you mean you're not connected to the SQL server? In that case you can't execute queries, so I'm wondering what exactly you're doing.

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

                G S 2 Replies Last reply
                0
                • jsulmJ jsulm

                  @Samuel-Ives said in QDateTime fromString () does not work:

                  but it's strange that the result of the query is with a 'T' in the timestamp while the server is not present

                  Do you mean you're not connected to the SQL server? In that case you can't execute queries, so I'm wondering what exactly you're doing.

                  G Offline
                  G Offline
                  Gerd
                  wrote on last edited by
                  #8

                  @VRonin: good point...
                  @Samuel-Ives : what do you mean with "server is not present" ? There should be no query run against a non present server...

                  1 Reply Last reply
                  0
                  • jsulmJ jsulm

                    @Samuel-Ives said in QDateTime fromString () does not work:

                    but it's strange that the result of the query is with a 'T' in the timestamp while the server is not present

                    Do you mean you're not connected to the SQL server? In that case you can't execute queries, so I'm wondering what exactly you're doing.

                    S Offline
                    S Offline
                    Samuel Ives
                    wrote on last edited by
                    #9

                    @jsulm @Gerd I believe it is a translation error, I meant that in the database vizualization, there is no such 'T' in the time record

                    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