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. 12 Time Format to 24 Hour Time Format in using DateTime class

12 Time Format to 24 Hour Time Format in using DateTime class

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 5 Posters 6.1k 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.
  • M Offline
    M Offline
    Mahesh Arrajella
    wrote on last edited by
    #1

    how to change 12 Time Format to 24 Hour Time Format in using DateTime class after getting time from system

    1 Reply Last reply
    0
    • Paul ColbyP Offline
      Paul ColbyP Offline
      Paul Colby
      wrote on last edited by
      #2

      Hi @Mahesh-Arrajella,

      QDateTime only differentiates between 12 and 24 hour time when parsing / converting to and from strings. I'm guessing you're referring to the format that is output, such as using the << operator, or QDateTime::toString() - by default, these use a system-dependant format (eg based on what he current logged in user has configured for their system preferences), however it can be changed to be 12 or 24 hours explicitly by passing parameters to QDateTime::toString(). For example:

          const QDateTime date = QDateTime::currentDateTime();
          qDebug() << date;
          qDebug() << date.toString();
          qDebug() << date.toString("hh:mm");
          qDebug() << date.toString("h:mm a");
      

      Output (on my system):

      QDateTime(2017-12-28 20:30:04.412 AEDT Qt::TimeSpec(LocalTime))
      "Thu. Dec. 28 20:30:04 2017"
      "20:30"
      "8:30 pm"
      

      Note that the second line is dependant on my current system preferences, whereas the last two lines are system-independent (though still in my local timezone).

      Hopefully that's what you were asking about, but if not, give us a bit more detail about what you're trying to achieve, ideally with some sample code, and we'll see if we can be more helpful :)

      Cheers.

      1 Reply Last reply
      1
      • M Offline
        M Offline
        Mahesh Arrajella
        wrote on last edited by
        #3

        But it is displaying on console, but i want to show in QDateTime Widget

        T Paul ColbyP VRoninV 3 Replies Last reply
        0
        • M Mahesh Arrajella

          But it is displaying on console, but i want to show in QDateTime Widget

          T Offline
          T Offline
          Tirupathi Korla
          wrote on last edited by
          #4
          This post is deleted!
          1 Reply Last reply
          0
          • M Offline
            M Offline
            Mahesh Arrajella
            wrote on last edited by
            #5

            ui->dateTimeEdit->setDateTime(date);
            Giving an error

            1 Reply Last reply
            0
            • M Mahesh Arrajella

              But it is displaying on console, but i want to show in QDateTime Widget

              Paul ColbyP Offline
              Paul ColbyP Offline
              Paul Colby
              wrote on last edited by
              #6

              @Mahesh-Arrajella said in 12 Time Format to 24 Hour Time Format in using DateTime class:

              but i want to show in QDateTime Widget

              QDateTime is not a widget. Perhaps you mean QDateTimeEdit? In which case you can use QDateTimeEdit::setDisplayFormat().

              M 1 Reply Last reply
              3
              • mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #7

                hi
                as @Paul-Colby says
                alt text

                1 Reply Last reply
                2
                • M Mahesh Arrajella

                  But it is displaying on console, but i want to show in QDateTime Widget

                  VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by VRonin
                  #8

                  @Mahesh-Arrajella said in 12 Time Format to 24 Hour Time Format in using DateTime class:

                  QDateTime Widget

                  // QDateTimeEdit* ui->dateTimeEdit;
                  ui->dateTimeEdit->setDisplayFormat(ui->dateTimeEdit->locale().dateFormat() + " HH:mm");
                  ui->dateTimeEdit->setDateTime(QDateTime::currentDateTime());
                  

                  "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
                  • Paul ColbyP Paul Colby

                    @Mahesh-Arrajella said in 12 Time Format to 24 Hour Time Format in using DateTime class:

                    but i want to show in QDateTime Widget

                    QDateTime is not a widget. Perhaps you mean QDateTimeEdit? In which case you can use QDateTimeEdit::setDisplayFormat().

                    M Offline
                    M Offline
                    Mahesh Arrajella
                    wrote on last edited by
                    #9

                    @Paul-Colby ui->dateTimeEdit->setDateTime(date.toString("hh:mm")); is giving an error

                    T Paul ColbyP 2 Replies Last reply
                    0
                    • M Mahesh Arrajella

                      @Paul-Colby ui->dateTimeEdit->setDateTime(date.toString("hh:mm")); is giving an error

                      T Offline
                      T Offline
                      Tirupathi Korla
                      wrote on last edited by
                      #10

                      @Mahesh-Arrajella
                      ui->dateTimeEdit->setDateTime(date);

                      1 Reply Last reply
                      0
                      • M Mahesh Arrajella

                        @Paul-Colby ui->dateTimeEdit->setDateTime(date.toString("hh:mm")); is giving an error

                        Paul ColbyP Offline
                        Paul ColbyP Offline
                        Paul Colby
                        wrote on last edited by
                        #11

                        @Mahesh-Arrajella said in 12 Time Format to 24 Hour Time Format in using DateTime class:

                        ui->dateTimeEdit->setDateTime(date.toString("hh:mm")); is giving an error

                        As @VRonin said:

                        ui->dateTimeEdit->setDisplayFormat("hh:mm");
                        ui->dateTimeEdit->setDateTime(date);
                        

                        Cheers.

                        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