Important: Please read the Qt Code of Conduct -

QML date and time manipulation

  • There seems to be no straightforward date and time manipulation / conversion facility as in Qt C++.

    My project requires me to get the current date and find out what the current month day and year are in the "yyMMdd" format using the current date/time. I find there not a well documented indication as to how to go about this even though I would think it is a common task.

    The QML documentation says to use Qt.formatDateTime(datetime date, format variant) to return a string, which seems to be a reformat of the C++ QString formatting functions.

    Well the problem is there is no straightforward way indicated in the docs to obtain the current datetime to format as available with QDate or QDateTime currentDate(). I have tried to use the date variable type and update it to but that is the wrong data type for formatDateTime(). The javascripte Date() and Date().now are strings not datetime data types.

    There are apparent holes in the documentation on date time functions in QML / Quick / JavaScript? Can someone help a Qt C++ programmer and newcomer to QML?

  • use Javascript's date
    var today = new Date() // contains current date and time

  • Hi,

    As Kxyu mentions, you should be able to use the JS Date object (it's internally converted to a QDateTime). For example, you can display a formatted date like this:

    import Qt 4.7

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


  • Since I wanted to manipulate the Date() value and not the string I had to construct a javascript function to do just that. QML does not expose all the methods of QDateTime.

    function getYesterday( ) {
    var today = new Date();
    var dateTime = new Date( Qt.formatDate(today, "yy/MM/dd") );

    dateTime.setMinutes( Qt.formatDateTime ( today, "mm" )  );
    dateTime.setHours( Qt.formatDateTime ( today, "hh" )-24 );
    return Qt.formatDate(dateTime, "yyMMdd");

    The string returned is the value needed.
    I would have preferred to do it all in QML the way C++ makes these functions available.

  • man, you're insane or something. you function returns crap
    I even don't know, if I understood you right, but this returns yesterday's date like yyMMdd

    function getYesterday( ) {
    var today = new Date();
    return Qt.formatDate(today, "yyMMdd");


  • Please don't be offending. We all want to be treated respectful.


  • Yes you are right. The function DOES return the date in the "yyMMdd" format. That is because it is part of a URL string that requires it, not because it is used in a normal date string.

  • hi guys,

    i got a problem with time format code,
    for example: i would like to display Asian, European timings.
    Is their any code, please give me the syntax to display any country timings.

  • Hi,

    Take a look at QLocale. You might need to create a helper QML type with a Q_INVOKABLE function with a signature like: QString formattedDateForLocale(const QDateTime &dt, const QString &localeIdentifier).


  • hey chris,

    Well, i am interested in javascript. i think, for your suggestions need c++.

    can please fetch it in javascript, if have any related tutorial, i would be more happy for your concern. I am a newbie to QML.


  • Qt5 and javascript are very easy to use now. If you have not upgraded, now is the time. See the Qt5 Docs.

Log in to reply