Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Error with QDateEdit and current time
Qt 6.12 LTS is out! See what's new in the release blog

Error with QDateEdit and current time

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 2 Posters 1.6k 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.
  • C Offline
    C Offline
    Carque
    wrote on last edited by
    #1

    I have the following example code in c++

    QDate fecha = QDate::currentDate();
    QDateEdit *entrada = new QDateEdit(fecha);
    Registro::Aviso(fecha.toString().toStdString());
    Registro::Aviso(entrada->date().toString().toStdString());
    
    

    It worked perfectly until September 11, 2022 that shows the date of September 14, 1752, today it works fine again, I am in the chile/continental time zone, on September 10 the time was changed from 11:59 p.m. 01:00

    Terminal output (September 10, 2022)

    [2022-09-10 18:14:14][AVISO] sáb sep. 10 2022
    [2022-09-10 18:14:14][AVISO] sáb sep. 10 2022
    

    Terminal output (September 11, 2022)

    [2022-09-11 18:00:10][AVISO] dom sep. 11 2022
    [2022-09-11 18:00:10][AVISO] jue sep. 14 1752
    

    Terminal output (September 12, 2022)

    [2022-09-12 18:14:45][AVISO] lun sep. 12 2022
    [2022-09-12 18:14:45][AVISO] lun sep. 12 2022
    
    JonBJ 1 Reply Last reply
    0
    • C Carque

      I have the following example code in c++

      QDate fecha = QDate::currentDate();
      QDateEdit *entrada = new QDateEdit(fecha);
      Registro::Aviso(fecha.toString().toStdString());
      Registro::Aviso(entrada->date().toString().toStdString());
      
      

      It worked perfectly until September 11, 2022 that shows the date of September 14, 1752, today it works fine again, I am in the chile/continental time zone, on September 10 the time was changed from 11:59 p.m. 01:00

      Terminal output (September 10, 2022)

      [2022-09-10 18:14:14][AVISO] sáb sep. 10 2022
      [2022-09-10 18:14:14][AVISO] sáb sep. 10 2022
      

      Terminal output (September 11, 2022)

      [2022-09-11 18:00:10][AVISO] dom sep. 11 2022
      [2022-09-11 18:00:10][AVISO] jue sep. 14 1752
      

      Terminal output (September 12, 2022)

      [2022-09-12 18:14:45][AVISO] lun sep. 12 2022
      [2022-09-12 18:14:45][AVISO] lun sep. 12 2022
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #3

      @Carque said in Error with QDateEdit and current time:

      I am in the chile/continental time zone, on September 10 the time was changed from 11:59 p.m. 01:00

      That means that on Sep 10/11 a local time between 00:00 and 01:00 "does not exist", there was no 00:00 or 00:30, agreed? I imagine in some shape or form you passed such an "illegal" datetime to a Qt datetime routine, and got back "illegal" from it. (See bool QDateTime::isValid() const and the notes there.) That probably returned something like a datetime of 0, and I would imagine that maps to the year 1752, hence your output.

      I am guessing that QDate treats the time element as 00:00. You will have to do something about this to get a correct conversion.

      C 1 Reply Last reply
      1
      • C Offline
        C Offline
        Carque
        wrote on last edited by Carque
        #2

        I found an approximation of solution, don't pass the date through the constructor and use the setDate method instead.
        because? I dont know

        QDate fecha = QDate::currentDate();
        QDateEdit *entrada = new QDateEdit();
        entrada->setDate(fecha);
        

        The other problem with this was

        QDate local_date = QDate::fromString("2022-09-11", "yyyy-MM-dd");
        

        this returns year 0

        instead I had to use

        QDate local_date = QDate::fromString("2022-09-11T00:00:00", Qt::ISODate);
        

        because as JonB says it is not a valid time, but I am not interested in the time, I am only interested in the date, I am going to have to read the local date and save it as a utc date?.

        1 Reply Last reply
        0
        • C Carque

          I have the following example code in c++

          QDate fecha = QDate::currentDate();
          QDateEdit *entrada = new QDateEdit(fecha);
          Registro::Aviso(fecha.toString().toStdString());
          Registro::Aviso(entrada->date().toString().toStdString());
          
          

          It worked perfectly until September 11, 2022 that shows the date of September 14, 1752, today it works fine again, I am in the chile/continental time zone, on September 10 the time was changed from 11:59 p.m. 01:00

          Terminal output (September 10, 2022)

          [2022-09-10 18:14:14][AVISO] sáb sep. 10 2022
          [2022-09-10 18:14:14][AVISO] sáb sep. 10 2022
          

          Terminal output (September 11, 2022)

          [2022-09-11 18:00:10][AVISO] dom sep. 11 2022
          [2022-09-11 18:00:10][AVISO] jue sep. 14 1752
          

          Terminal output (September 12, 2022)

          [2022-09-12 18:14:45][AVISO] lun sep. 12 2022
          [2022-09-12 18:14:45][AVISO] lun sep. 12 2022
          
          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #3

          @Carque said in Error with QDateEdit and current time:

          I am in the chile/continental time zone, on September 10 the time was changed from 11:59 p.m. 01:00

          That means that on Sep 10/11 a local time between 00:00 and 01:00 "does not exist", there was no 00:00 or 00:30, agreed? I imagine in some shape or form you passed such an "illegal" datetime to a Qt datetime routine, and got back "illegal" from it. (See bool QDateTime::isValid() const and the notes there.) That probably returned something like a datetime of 0, and I would imagine that maps to the year 1752, hence your output.

          I am guessing that QDate treats the time element as 00:00. You will have to do something about this to get a correct conversion.

          C 1 Reply Last reply
          1
          • JonBJ JonB

            @Carque said in Error with QDateEdit and current time:

            I am in the chile/continental time zone, on September 10 the time was changed from 11:59 p.m. 01:00

            That means that on Sep 10/11 a local time between 00:00 and 01:00 "does not exist", there was no 00:00 or 00:30, agreed? I imagine in some shape or form you passed such an "illegal" datetime to a Qt datetime routine, and got back "illegal" from it. (See bool QDateTime::isValid() const and the notes there.) That probably returned something like a datetime of 0, and I would imagine that maps to the year 1752, hence your output.

            I am guessing that QDate treats the time element as 00:00. You will have to do something about this to get a correct conversion.

            C Offline
            C Offline
            Carque
            wrote on last edited by
            #4

            @JonB How can I save a local date as utc without saving the time? I think I can't because it would end up confusing days. but the invoice do not have hour and I can not invent one

            JonBJ 1 Reply Last reply
            0
            • C Carque

              @JonB How can I save a local date as utc without saving the time? I think I can't because it would end up confusing days. but the invoice do not have hour and I can not invent one

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

              @Carque
              Sorry, I don't know what you mean. You can't have a "local" date in, or to convert to, UTC without involving the time, because UTC/timezones involve times.

              C 1 Reply Last reply
              0
              • JonBJ JonB

                @Carque
                Sorry, I don't know what you mean. You can't have a "local" date in, or to convert to, UTC without involving the time, because UTC/timezones involve times.

                C Offline
                C Offline
                Carque
                wrote on last edited by Carque
                #6

                @JonB I know but QDateEdit insists on passing me a date from utc to local adding 00:00:00 as the time and it changes me from 2022-09-11 to 2022-09-10, I think the easiest solution is to tell it that this all in utc

                m_fecha = new QDateEdit(this);
                m_fecha->setTimeSpec(Qt::UTC);
                m_fecha->setDate(QDate::currentDate());
                
                JonBJ 1 Reply Last reply
                0
                • C Carque

                  @JonB I know but QDateEdit insists on passing me a date from utc to local adding 00:00:00 as the time and it changes me from 2022-09-11 to 2022-09-10, I think the easiest solution is to tell it that this all in utc

                  m_fecha = new QDateEdit(this);
                  m_fecha->setTimeSpec(Qt::UTC);
                  m_fecha->setDate(QDate::currentDate());
                  
                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #7

                  @Carque said in Error with QDateEdit and current time:

                  I think the easiest solution is to tell it that this all in utc

                  Indeed so! If you can afford to always work in UTC --- and if you're only interested in dates without times that seems reasonable anyway --- you avoid those pesky timezone conversions which can mess up the date.

                  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
                  • Unsolved