(Difference between revisions)

Get current Date and Time in Qt

From Nokia Developer Wiki
Jump to: navigation, search
hamishwillee (Talk | contribs)
m (Text replace - "Category:MeeGo" to "Category:MeeGo Harmattan")
hamishwillee (Talk | contribs)
m (Text replace - "<code cpp>" to "<code cpp-qt>")
Line 50: Line 50:
===Headers required===
===Headers required===
<code cpp>
<code cpp-qt>
#include <QDateTime>
#include <QDateTime>
Line 56: Line 56:
<code cpp>
<code cpp-qt>
SystemTime::SystemTime(QWidget *parent)
SystemTime::SystemTime(QWidget *parent)
     : QMainWindow(parent)
     : QMainWindow(parent)

Latest revision as of 04:16, 11 October 2012

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.

Article Metadata
Code ExampleTested with
Devices(s): Nokia 5800 XpressMusic
Platform(s): S60 3rd Edition, FP1, FP2
S60 5th Edition
Platform Security
Signing Required: Self-Signed
Capabilities: None
Keywords: QDateTime::currentDateTime(), QDate::currentDate(), QTime::currentTime()
Created: savaj (11 Jun 2009)
Updated: hamishwillee (20 Feb 2012)
Last edited: hamishwillee (11 Oct 2012)


[edit] QML

The current date/time can be obtained in QML using Javascript’s date:

var today = new Date() // contains current date and time

QML provides the Qt.formatDate() and Qt.formatDateTime() methods for formatting this value. You can also dynamically declare dates using the QML date basic type.

An example displaying the current date in YYMMDD format might look like:

Rectangle {
width: 200
height: 200
Text {
anchors.centerIn: parent
text: Qt.formatDateTime(new Date(), "yyMMdd")

[edit] Qt C++ API

The main Qt C++ methods for getting the current date and time are:

[edit] Headers required

#include <QDateTime>

[edit] Source

SystemTime::SystemTime(QWidget *parent)
: QMainWindow(parent)
setWindowTitle("System Time");
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);

[edit] Screenshot

The code snippet is expected to show current date and time of device.


[edit] Code Example

  • The Code Example will show current date and time on screen and example is tested on Nokia 5800 XpressMusic.
This page was last modified on 11 October 2012, at 04:16.
1850 page views in the last 30 days.