Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.
Get current Date and Time in Qt
This code example shows how to get current date and time using Qt C++ and QML. Note that the code example shows how to use Qt C++ APIs, while only code snippets are provided for QML.
S60 5th Edition
var today = new Date() // contains current date and time
An example displaying the current date in YYMMDD format might look like:
text: Qt.formatDateTime(new Date(), "yyMMdd")
Qt C++ API
The main Qt C++ methods for getting the current date and time are:
- QDateTime::currentDateTime() returns the current date and time, as reported by the system clock, in the local time zone.
- QDate::currentDate() returns the current date, as reported by the system clock, in the local time zone.
- QTime::currentTime() returns the current time, as reported by the system clock, in the local time zone.
ui.statusbar->showMessage("Showing system time");
//get current date
QDate date = QDate::currentDate();
QString dateString = date.toString();
ui.label->setText("Date: " + dateString);
//get current time
QTime time = QTime::currentTime();
QString timeString = time.toString();
ui.label_2->setText("Time: " + timeString);
//get current date and time
QDateTime dateTime = QDateTime::currentDateTime();
QString dateTimeString = dateTime.toString();
ui.label_3->setText("Date and Time: " + dateTimeString);
The code snippet is expected to show current date and time of device.
- The Code Example will show current date and time on screen and example is tested on Nokia 5800 XpressMusic.