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::currentDateTime() sometimes return ???
Forum Update on Monday, May 27th 2025

QDateTime::currentDateTime() sometimes return ???

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 4 Posters 7.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.
  • A Offline
    A Offline
    asmant
    wrote on last edited by asmant
    #1

    Hello, in my windows app's logs I get current date-time like this:

    QDateTime dateTime = dateTime.currentDateTime();
    QString currentDateTime = dateTime.toString("yyyy-MM-dd HHmmss");
    

    It worked well until I noticed that sometimes the time is ????-??-?? ??:??:??. How it can be that way? I know that windows time can be incorrect, but it should return incorrect time and not ???

    jsulmJ 1 Reply Last reply
    0
    • A asmant

      Hello, in my windows app's logs I get current date-time like this:

      QDateTime dateTime = dateTime.currentDateTime();
      QString currentDateTime = dateTime.toString("yyyy-MM-dd HHmmss");
      

      It worked well until I noticed that sometimes the time is ????-??-?? ??:??:??. How it can be that way? I know that windows time can be incorrect, but it should return incorrect time and not ???

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

      @asmant How do you write currentDateTime to the log?

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

      A 1 Reply Last reply
      1
      • jsulmJ jsulm

        @asmant How do you write currentDateTime to the log?

        A Offline
        A Offline
        asmant
        wrote on last edited by
        #3

        @jsulm

           QDateTime dateTime = dateTime.currentDateTime();
           QFile file(filePath);
        
           QTextStream out(&file);
           out << dateTime.toString("yyyy-MM-dd HH:mm:ss") << endl;
           out << m_logData;
        
        1 Reply Last reply
        0
        • A Offline
          A Offline
          asmant
          wrote on last edited by
          #4

          So, I find out that, for example if the windows language is Egypt(arabic) so the time & date is displayed not in english numbers. It is in Egypt(arabic) writing. Is there a function to get (format) time & date in English language numbers?

          J.HilkJ 1 Reply Last reply
          0
          • A asmant

            So, I find out that, for example if the windows language is Egypt(arabic) so the time & date is displayed not in english numbers. It is in Egypt(arabic) writing. Is there a function to get (format) time & date in English language numbers?

            J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by J.Hilk
            #5

            @asmant
            That's what QLocale is for :)

            QLocale::dateTimeFormat


            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.

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

              @asmant
              That's what QLocale is for :)

              QLocale::dateTimeFormat

              A Offline
              A Offline
              asmant
              wrote on last edited by asmant
              #6

              @J-Hilk I have set locale to English in the main.cpp. Do I have to do it somewhere else?

                 QLocale locale = QLocale(QLocale::English);
                 QLocale::setDefault(locale);
              
              Cobra91151C 1 Reply Last reply
              0
              • A asmant

                @J-Hilk I have set locale to English in the main.cpp. Do I have to do it somewhere else?

                   QLocale locale = QLocale(QLocale::English);
                   QLocale::setDefault(locale);
                
                Cobra91151C Offline
                Cobra91151C Offline
                Cobra91151
                wrote on last edited by Cobra91151
                #7

                @asmant

                Hello!

                @J-Hilk meant to use the dateTimeFormat (https://doc.qt.io/qt-5/qlocale.html#dateTimeFormat) function, it will return the date format used for the current locale.

                Also, you can try out my solution:

                QLocale testLocale = QLocale(QLocale::English, QLocale::UnitedStates);
                QString dateTimeText = testLocale.toString(QDateTime::currentDateTime(), "dddd / dd / MMMM / yyyy / hh:mm:ss AP");
                qDebug() << dateTimeText;
                

                It will display date time using English locale (United States). You can change QLocale and format it to your needs. Happy coding!

                A 1 Reply Last reply
                5
                • Cobra91151C Cobra91151

                  @asmant

                  Hello!

                  @J-Hilk meant to use the dateTimeFormat (https://doc.qt.io/qt-5/qlocale.html#dateTimeFormat) function, it will return the date format used for the current locale.

                  Also, you can try out my solution:

                  QLocale testLocale = QLocale(QLocale::English, QLocale::UnitedStates);
                  QString dateTimeText = testLocale.toString(QDateTime::currentDateTime(), "dddd / dd / MMMM / yyyy / hh:mm:ss AP");
                  qDebug() << dateTimeText;
                  

                  It will display date time using English locale (United States). You can change QLocale and format it to your needs. Happy coding!

                  A Offline
                  A Offline
                  asmant
                  wrote on last edited by
                  #8

                  @Cobra91151 That's what I wanted to do. Thank you!

                  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