12 Time Format to 24 Hour Time Format in using DateTime class
-
how to change 12 Time Format to 24 Hour Time Format in using DateTime class after getting time from system
-
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, orQDateTime::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 toQDateTime::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.
-
But it is displaying on console, but i want to show in QDateTime Widget
-
This post is deleted!
-
ui->dateTimeEdit->setDateTime(date);
Giving an error -
@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 meanQDateTimeEdit
? In which case you can use QDateTimeEdit::setDisplayFormat(). -
hi
as @Paul-Colby says
-
@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());
-
@Paul-Colby ui->dateTimeEdit->setDateTime(date.toString("hh:mm")); is giving an error
-
@Mahesh-Arrajella
ui->dateTimeEdit->setDateTime(date); -
@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.