Date QML Type

Provides date functions. More...

Import Statement: import QtQml 2.15

Methods

Detailed Description

The QML Date object extends the JS Date object with locale aware functions.

Functions that accept a format argument take either Locale.LongFormat, Locale.ShortFormat, Locale.NarrowFormat enumeration values, or a string specifying the format.

Format Enumeration Values

Use the enumeration values when you want a format that matches the locale preferences.

Locale.LongFormatLonger format
Locale.ShortFormatShorter format
Locale.NarrowFormatIn this context same as Locale.ShortFormat

The format that the enumerations represent will depend on your locale, but also the method that the enumeration is used for.

For example, for the en_US locale, these format strings are used:

FunctionLocale EnumFormat String
fromLocaleDateString, toLocaleDateStringLocale.LongFormatdddd, MMMM d, yyyy
fromLocaleDateString, toLocaleDateStringLocale.ShortFormatM/d/yy
fromLocaleTimeString, toLocaleTimeStringLocale.LongFormath:mm:ss AP t
fromLocaleTimeString, toLocaleTimeStringLocale.ShortFormath:mm AP
fromLocaleString, toLocaleStringLocale.LongFormatdddd, MMMM d, yyyy h:mm:ss AP t
fromLocaleString, toLocaleStringLocale.ShortFormatM/d/yy h:mm AP

Format Strings

You can also directly specify a format string.

These expressions may be used for format dates:

ExpressionOutput
dthe day as number without a leading zero (1 to 31)
ddthe day as number with a leading zero (01 to 31)
dddthe abbreviated localized day name (e.g. 'Mon' to 'Sun').
ddddthe long localized day name (e.g. 'Monday' to 'Sunday').
Mthe month as number without a leading zero (1 to 12)
MMthe month as number with a leading zero (01 to 12)
MMMthe abbreviated localized month name (e.g. 'Jan' to 'Dec').
MMMMthe long localized month name (e.g. 'January' to 'December').
yythe year as two digit number (00 to 99)
yyyythe year as four digit number. If the year is negative, a minus sign is prepended in addition.

All other input characters will be ignored. Any sequence of characters that are enclosed in singlequotes will be treated as text and not be used as an expression. Two consecutive singlequotes ("''") are replaced by a singlequote in the output.

Example format strings (assuming that the Date is the 20 July 1969):

FormatResult
dd.MM.yyyy20.07.1969
ddd MMMM d yySun July 20 69
'The day is' ddddThe day is Sunday

These expressions may be used for formatting time:

ExpressionOutput
hthe hour without a leading zero (0 to 23 or 1 to 12 if AM/PM display)
hhthe hour with a leading zero (00 to 23 or 01 to 12 if AM/PM display)
Hthe hour without a leading zero (0 to 23, even with AM/PM display)
HHthe hour with a leading zero (00 to 23, even with AM/PM display)
mthe minute without a leading zero (0 to 59)
mmthe minute with a leading zero (00 to 59)
sthe second without a leading zero (0 to 59)
ssthe second with a leading zero (00 to 59)
zthe milliseconds without leading zeroes (0 to 999)
zzzthe milliseconds with leading zeroes (000 to 999)
AP or Ause AM/PM display. AP will be replaced by either "AM" or "PM".
ap or ause am/pm display. ap will be replaced by either "am" or "pm".
tthe timezone (for example "CEST")

All other input characters will be ignored. Any sequence of characters that are enclosed in singlequotes will be treated as text and not be used as an expression. Two consecutive singlequotes ("''") are replaced by a singlequote in the output.

Example format strings (assuming that the QTime is 14:13:09.042)

FormatResult
hh:mm:ss.zzz14:13:09.042
h:m:s ap2:13:9 pm
H:m:s a14:13:9 pm

If the date is invalid, an empty string will be returned.

Further Notes

Using the locale-aware functions to perform date or time formatting can result in incorrectly formatted times, due to an inconsistency in specification between Qt and JS. ECMA-262 specifies that historical dates should be intrepreted by projecting the current rules for daylight-saving onto past years, while Qt uses historical data (where available) to determine whether daylight-saving was in effect for a given date. Therefore, constructing a Date value in JS and converting it to a string using the locale-aware functions can yield a result incorrect by one hour, if DST is currently in effect, while it was not for the time specified, or vice versa.

There are different date formats with different understandings of negative years. Common human language does not have a year 0. The year after 1BC is 1AD. This understanding is reflected when printing or parsing dates in one of the formats not standardized by ECMAScript. That is: toString(), toLocaleString(), toUTCString() and friends. ECMAScript does standardize one format: ISO 8601. This is what you get when you call toISOString(). This format does include a year 0, which is 1BC in other formats. Thus you get different years when printing negative dates with toISOString() and toString().

When setting the year using the Date constructor or set(UTC)FullYear(), the convention set by ISO 8601 is used and 0 is a valid year. This means negative years set with the constructor or set(UTC)FullYear() are zero-based and thus offset by one year from what is printed with toString() and friends. Parsing the output of any of the to*String() methods will yield the same date value you printed from. Date.parse() will recognize the different formats and their convention on the existence of year 0.

Note that all this is different from what you get in other JavaScript implementations which usually treat year 0 as valid in all string representations. As the date formats are "implementation-dependent" in the ECMAScript standard, this is still valid, though.

See also Locale.

Method Documentation

string fromLocaleDateString(locale, dateString, format)

Converts the date string dateString to a Date object using locale and format.

If format is not specified, Locale.LongFormat will be used.

If locale is not specified, the default locale will be used.

The following example shows the current date first being formatted as a date string using the default locale and format, then parsed back again in the same manner:

import QtQml 2.0

QtObject {
    property var locale: Qt.locale()
    property date currentDate: new Date()
    property string dateString

    Component.onCompleted: {
        dateString = currentDate.toLocaleDateString();
        print(Date.fromLocaleDateString(dateString));
    }
}

string fromLocaleString(locale, dateTimeString, format)

Converts the datetime string dateTimeString to a Date object using locale and format.

If format is not specified, Locale.LongFormat will be used.

If locale is not specified, the default locale will be used.

The following example shows a datetime being parsed from a datetime string in a certain format using the default locale:

import QtQml 2.0

QtObject {
    property var locale: Qt.locale()
    property string dateTimeString: "Tue 2013-09-17 10:56:06"

    Component.onCompleted: {
        print(Date.fromLocaleString(locale, dateTimeString, "ddd yyyy-MM-dd hh:mm:ss"));
    }
}

string fromLocaleTimeString(locale, timeString, format)

Converts the time string timeString to a Date object using locale and format.

If format is not specified, Locale.LongFormat will be used.

If locale is not specified, the default locale will be used.

The following example shows the current time first being formatted as a time string using the default locale and a short format, then parsed back again in the same manner:

import QtQml 2.2

QtObject {
    property var locale: Qt.locale()
    property date currentTime: new Date()
    property string timeString

    Component.onCompleted: {
        timeString = currentTime.toLocaleTimeString(locale, Locale.ShortFormat);
        print(Date.fromLocaleTimeString(locale, timeString, Locale.ShortFormat));
    }
}

string timeZoneUpdated()

Informs the JS engine that the system's timezone has been changed, which is necessary for the correct manipulation of datetime data.

JS stores Date objects in UTC time; all access to and from Date components in local time involves the application of the current offset from UTC. If the current offset changes due to the timezone being updated, the JS engine needs to be informed so that it can recalculate the offset.

This function should be called after the system's timezone has been updated.

For example, an application that changes the timezone would call timeZoneUpdated() after setting the new time zone:

property string selectedTimeZone

onSelectedTimeZoneChanged: {
    MyFunctions.setSystemTimeZone(selectedTimeZone)
    Date.timeZoneUpdated()
}

string toLocaleDateString(locale, format)

Converts the Date to a string containing the date suitable for the specified locale in the specified format.

If format is not specified, Locale.LongFormat will be used.

If locale is not specified, the default locale will be used.

The following example shows the current date formatted for the German locale:

import QtQuick 2.0

Text {
    text: "The date is: " + new Date().toLocaleDateString(Qt.locale("de_DE"))
}

string toLocaleString(locale, format)

Converts the Date to a string containing the date and time suitable for the specified locale in the specified format.

If format is not specified, Locale.LongFormat will be used.

If locale is not specified, the default locale will be used.

The following example shows the current date and time formatted for the German locale:

import QtQuick 2.0

Text {
    text: "The date is: " + new Date().toLocaleString(Qt.locale("de_DE"))
}

string toLocaleTimeString(locale, format)

Converts the Date to a string containing the time suitable for the specified locale in the specified format.

If format is not specified, Locale.LongFormat will be used.

If locale is not specified, the default locale will be used.

The following example shows the current time formatted for the German locale:

import QtQuick 2.0

Text {
    text: "The date is: " + new Date().toLocaleTimeString(Qt.locale("de_DE"))
}

© 2024 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.