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 ???
QtWS25 Last Chance

QDateTime::currentDateTime() sometimes return ???

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 4 Posters 7.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.
  • A Offline
    A Offline
    asmant
    wrote on 1 Oct 2019, 06:47 last edited by asmant 10 Jan 2019, 06:48
    #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 ???

    J 1 Reply Last reply 1 Oct 2019, 07:11
    0
    • A asmant
      1 Oct 2019, 06:47

      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 ???

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 1 Oct 2019, 07:11 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 Oct 2019, 07:34
      1
      • J jsulm
        1 Oct 2019, 07:11

        @asmant How do you write currentDateTime to the log?

        A Offline
        A Offline
        asmant
        wrote on 1 Oct 2019, 07:34 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 14 Oct 2019, 10:40 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 1 Reply Last reply 14 Oct 2019, 10:44
          0
          • A asmant
            14 Oct 2019, 10:40

            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 Offline
            J Offline
            J.Hilk
            Moderators
            wrote on 14 Oct 2019, 10:44 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 14 Oct 2019, 12:39
            3
            • J J.Hilk
              14 Oct 2019, 10:44

              @asmant
              That's what QLocale is for :)

              QLocale::dateTimeFormat

              A Offline
              A Offline
              asmant
              wrote on 14 Oct 2019, 12:39 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);
              
              C 1 Reply Last reply 14 Oct 2019, 13:22
              0
              • A asmant
                14 Oct 2019, 12:39

                @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);
                
                C Offline
                C Offline
                Cobra91151
                wrote on 14 Oct 2019, 13:22 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 15 Oct 2019, 07:18
                5
                • C Cobra91151
                  14 Oct 2019, 13:22

                  @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 15 Oct 2019, 07:18 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