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.
  • P Offline
    P Offline
    Paul Colby
    wrote on 28 Dec 2017, 09:37 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 28 Dec 2017, 10:09 last edited by
      #3

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

      T P V 3 Replies Last reply 28 Dec 2017, 10:14
      0
      • M Mahesh Arrajella
        28 Dec 2017, 10:09

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

        T Offline
        T Offline
        Tirupathi Korla
        wrote on 28 Dec 2017, 10:14 last edited by
        #4
        This post is deleted!
        1 Reply Last reply
        0
        • M Offline
          M Offline
          Mahesh Arrajella
          wrote on 28 Dec 2017, 10:15 last edited by
          #5

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

          1 Reply Last reply
          0
          • M Mahesh Arrajella
            28 Dec 2017, 10:09

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

            P Offline
            P Offline
            Paul Colby
            wrote on 28 Dec 2017, 10:16 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 28 Dec 2017, 10:27
            3
            • M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 28 Dec 2017, 10:16 last edited by
              #7

              hi
              as @Paul-Colby says
              alt text

              1 Reply Last reply
              2
              • M Mahesh Arrajella
                28 Dec 2017, 10:09

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

                V Offline
                V Offline
                VRonin
                wrote on 28 Dec 2017, 10:17 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
                • P Paul Colby
                  28 Dec 2017, 10:16

                  @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 28 Dec 2017, 10:27 last edited by
                  #9

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

                  T P 2 Replies Last reply 28 Dec 2017, 10:33
                  0
                  • M Mahesh Arrajella
                    28 Dec 2017, 10:27

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

                    T Offline
                    T Offline
                    Tirupathi Korla
                    wrote on 28 Dec 2017, 10:33 last edited by
                    #10

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

                    1 Reply Last reply
                    0
                    • M Mahesh Arrajella
                      28 Dec 2017, 10:27

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

                      P Offline
                      P Offline
                      Paul Colby
                      wrote on 28 Dec 2017, 10:43 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

                      11/11

                      28 Dec 2017, 10:43

                      • Login

                      • Login or register to search.
                      11 out of 11
                      • First post
                        11/11
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved