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. How to det Date from String. QDate and Qtime?
Forum Updated to NodeBB v4.3 + New Features

How to det Date from String. QDate and Qtime?

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 3 Posters 670 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.
  • T Offline
    T Offline
    TheCeylann
    wrote on last edited by
    #1

    I have Date and Time values as String. I want to synchronize these values with QTime and Qdate and want the time to flow from the values I gave instead of Current time, how can I do this?

    Here is mu finction

    void MainWindow::on_syncdatebtn_clicked() 
    {
    
    
       QStringList dtstr = dtcal.split(".");
        dtcal.remove(".");
        serial.send(QString::fromUtf8("WR+1160")+dtcal);
        serial.waitdata();
        QString getdtcal=serial.getString();
         serial.waitdata();
         QStringList getdtcalbuf = getdtcal.split(",");
         qDebug()<< getdtcalbuf;
    QString Timeformat = getdtcalbuf[2]+"."+getdtcalbuf[3]+"."+getdtcalbuf[4];  
    QString Dateformat = getdtcalbuf[5]+"."+getdtcalbuf[6]+"."+getdtcalbuf[7];
    qDebug()<<Timeformat << "  "  << Dateformat;
         qDebug()<< dtcal;
         QTime Time =QTime::fromString(Timeformat,"hh.mm.ss");  
         QDate Date = QDate::fromString(Dateformat,"dd.MM.yy");
    qDebug()<< Time << "  "<<Date;  //QTime(Invalid)    QDate(Invalid)
    }
    
    1 Reply Last reply
    0
    • T TheCeylann

      @SGaist

      TimeFormat "21.25.15"  
      
      DateFormat  "2.10.22"
      

      So is there any other way I can set it to custom instead of the system clock?

      Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #9

      @TheCeylann said in How to det Date from String. QDate and Qtime?:

      DateFormat "2.10.22"

      This does not match the format "dd.MM.yy", also you should add 2000 to your year - QDateTime assumes 1900 if only two digits are given for the yeas.

      The time is correctly converted:

      QString Timeformat("21.25.15");
      QString Dateformat("02.10.22");
      QTime Time = QTime::fromString(Timeformat, "hh.mm.ss");
      QDate Date = QDate::fromString(Dateformat, "dd.MM.yy");
      qDebug() << Time << "  " << Date;
      --> QTime("21:25:15.000")    QDate("1922-10-02")
      

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      T 1 Reply Last reply
      1
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Hi,

        What is the content of your variables ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • T Offline
          T Offline
          TheCeylann
          wrote on last edited by TheCeylann
          #3

          @SGaist said in How to det Date from String. QDate and Qtime?:

          Değişkenlerinizin içeriği nedir?

          Hello
          getdtcalbuf returns me

          ("CMD", "OK", "21", "12", "57", "2", "10", "22", "DHC", "\r\n")
          

          This data comes to me from the serial port, I use the first two for control, the rest are in order;

          getdtcalbuf[2] = hour
          getdtcalbuf[3] = minute
          getdtcalbuf[4] = second
          getdtcalbuf[5] = day
          getdtcalbuf[6] = month
          getdtcalbuf[7] = year
          
          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #4

            And what is the content of your TimeFormat and DateFormat variables ?

            On a side note, these variable names are misleading. They are the date and time values not their format.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            T 1 Reply Last reply
            0
            • SGaistS SGaist

              And what is the content of your TimeFormat and DateFormat variables ?

              On a side note, these variable names are misleading. They are the date and time values not their format.

              T Offline
              T Offline
              TheCeylann
              wrote on last edited by
              #5

              @SGaist

              TimeFormat "21.25.15"  
              
              DateFormat  "2.10.22"
              

              So is there any other way I can set it to custom instead of the system clock?

              Christian EhrlicherC 1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #6

                The invalid date comes from a mismatch between your input string and the format.

                Are you sure that your time input does not suffer the same issue ?

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                T 1 Reply Last reply
                0
                • SGaistS SGaist

                  The invalid date comes from a mismatch between your input string and the format.

                  Are you sure that your time input does not suffer the same issue ?

                  T Offline
                  T Offline
                  TheCeylann
                  wrote on last edited by TheCeylann
                  #7

                  @SGaist It doesn't happen when I set the time via String. But when I do it with current DateTime() it works fine.
                  i'm Sorry I forgot myself.

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #8

                    Sorry, Turkish is not one of the language I speak.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    1
                    • T TheCeylann

                      @SGaist

                      TimeFormat "21.25.15"  
                      
                      DateFormat  "2.10.22"
                      

                      So is there any other way I can set it to custom instead of the system clock?

                      Christian EhrlicherC Offline
                      Christian EhrlicherC Offline
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on last edited by
                      #9

                      @TheCeylann said in How to det Date from String. QDate and Qtime?:

                      DateFormat "2.10.22"

                      This does not match the format "dd.MM.yy", also you should add 2000 to your year - QDateTime assumes 1900 if only two digits are given for the yeas.

                      The time is correctly converted:

                      QString Timeformat("21.25.15");
                      QString Dateformat("02.10.22");
                      QTime Time = QTime::fromString(Timeformat, "hh.mm.ss");
                      QDate Date = QDate::fromString(Dateformat, "dd.MM.yy");
                      qDebug() << Time << "  " << Date;
                      --> QTime("21:25:15.000")    QDate("1922-10-02")
                      

                      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                      Visit the Qt Academy at https://academy.qt.io/catalog

                      T 1 Reply Last reply
                      1
                      • Christian EhrlicherC Christian Ehrlicher

                        @TheCeylann said in How to det Date from String. QDate and Qtime?:

                        DateFormat "2.10.22"

                        This does not match the format "dd.MM.yy", also you should add 2000 to your year - QDateTime assumes 1900 if only two digits are given for the yeas.

                        The time is correctly converted:

                        QString Timeformat("21.25.15");
                        QString Dateformat("02.10.22");
                        QTime Time = QTime::fromString(Timeformat, "hh.mm.ss");
                        QDate Date = QDate::fromString(Dateformat, "dd.MM.yy");
                        qDebug() << Time << "  " << Date;
                        --> QTime("21:25:15.000")    QDate("1922-10-02")
                        
                        T Offline
                        T Offline
                        TheCeylann
                        wrote on last edited by
                        #10

                        @Christian-Ehrlicher Thank you. It works

                        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