Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
date.qdoc
Go to the documentation of this file.
1
// Copyright (C) 2021 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4
/*!
5
\page qml-qtqml-date.html
6
\title The Date JavaScript Object
7
\inqmlmodule QtQml
8
\brief Provides date functions.
9
10
The QML Date object extends the
11
\l{ECMAScript Specification of Date}{JS Date object} with
12
locale aware functions.
13
14
Functions that accept a \c format argument take either Locale.LongFormat,
15
Locale.ShortFormat, Locale.NarrowFormat enumeration values,
16
or a string specifying the format.
17
18
The form of the supported format strings is as described in the
19
documentation of \l QDate::toString(), \l QTime::toString() and \l
20
QDateTime::toString().
21
22
If the date is invalid, an empty string is returned.
23
24
\section1 Format Enumeration Values
25
26
Use the enumeration values when you want a format that matches the locale
27
preferences.
28
29
\table
30
\row \li Locale.LongFormat \li Longer format
31
\row \li Locale.ShortFormat \li Shorter format
32
\row \li Locale.NarrowFormat \li In this context same as Locale.ShortFormat
33
\endtable
34
35
The format that the enumerations represent will depend on your locale, but also
36
the method that the enumeration is used for.
37
38
For example, for the \c en_US locale, these format strings are used:
39
40
\table
41
\header \li Function \li Locale Enum \li Format String
42
\row \li fromLocaleDateString, toLocaleDateString \li Locale.LongFormat \li \c{dddd, MMMM d, yyyy}
43
\row \li fromLocaleDateString, toLocaleDateString \li Locale.ShortFormat \li \c{M/d/yy}
44
\row \li fromLocaleTimeString, toLocaleTimeString \li Locale.LongFormat \li \c{h:mm:ss AP t}
45
\row \li fromLocaleTimeString, toLocaleTimeString \li Locale.ShortFormat \li \c{h:mm AP}
46
\row \li fromLocaleString, toLocaleString \li Locale.LongFormat \li \c{dddd, MMMM d, yyyy h:mm:ss AP t}
47
\row \li fromLocaleString, toLocaleString \li Locale.ShortFormat \li \c{M/d/yy h:mm AP}
48
\endtable
49
50
\section1 Further Notes
51
52
Using the locale-aware functions to perform date or time formatting can
53
result in incorrectly formatted times, due to an inconsistency in specification
54
between Qt and JS. ECMA-262 specifies that historical dates should be intrepreted
55
by projecting the current rules for daylight-saving onto past years, while Qt uses
56
historical data (where available) to determine whether daylight-saving was in
57
effect for a given date. Therefore, constructing a Date value in JS and converting
58
it to a string using the locale-aware functions can yield a result incorrect by one
59
hour, if DST is currently in effect, while it was not for the time specified, or
60
vice versa.
61
62
There are different date formats with different understandings of negative years. Common
63
human language does not have a year 0. The year after 1BC is 1AD. This understanding is
64
reflected when printing or parsing dates in one of the formats not standardized by ECMAScript.
65
That is: toString(), toLocaleString(), toUTCString() and friends. ECMAScript does standardize
66
one format: ISO 8601. This is what you get when you call toISOString(). This format does include
67
a year 0, which is 1BC in other formats. Thus you get different years when printing negative
68
dates with toISOString() and toString().
69
70
When setting the year using the Date constructor or set(UTC)FullYear(), the convention set by
71
ISO 8601 is used and 0 is a valid year. This means negative years set with the constructor or
72
set(UTC)FullYear() are zero-based and thus offset by one year from what is printed with
73
toString() and friends. Parsing the output of any of the to*String() methods will yield the same
74
date value you printed from. Date.parse() will recognize the different formats and their
75
convention on the existence of year 0.
76
77
Note that all this is different from what you get in other JavaScript implementations which
78
usually treat year 0 as valid in all string representations. As the date formats are
79
"implementation-dependent" in the ECMAScript standard, this is still valid, though.
80
81
\sa {QtQml::Locale}{Locale}
82
83
\section1 string Date::toLocaleString(locale, format)
84
85
Converts the Date to a string containing the date and time
86
suitable for the specified \a locale
87
in the specified \a format.
88
89
If \a format is not specified, \l {QtQml::Locale}{Locale.LongFormat} will
90
be used.
91
92
If \a locale is not specified, the default locale will be used.
93
94
The following example shows the current date and time formatted
95
for the German locale:
96
\code
97
import QtQuick 2.0
98
99
Text {
100
text: "The date is: " + new Date().toLocaleString(Qt.locale("de_DE"))
101
}
102
\endcode
103
104
\section1 string Date::toLocaleDateString(locale, format)
105
106
Converts the Date to a string containing the date suitable for the specified \a locale
107
in the specified \a format.
108
109
If \a format is not specified, \l {QtQml::Locale}{Locale.LongFormat} will
110
be used.
111
112
If \a locale is not specified, the default locale will be used.
113
114
The following example shows the current date formatted
115
for the German locale:
116
\code
117
import QtQuick 2.0
118
119
Text {
120
text: "The date is: " + new Date().toLocaleDateString(Qt.locale("de_DE"))
121
}
122
\endcode
123
124
\section1 string Date::toLocaleTimeString(locale, format)
125
126
Converts the Date to a string containing the time suitable for the specified \a locale
127
in the specified \a format.
128
129
If \a format is not specified, \l {QtQml::Locale}{Locale.LongFormat} will
130
be used.
131
132
If \a locale is not specified, the default locale will be used.
133
134
The following example shows the current time formatted
135
for the German locale:
136
\code
137
import QtQuick 2.0
138
139
Text {
140
text: "The date is: " + new Date().toLocaleTimeString(Qt.locale("de_DE"))
141
}
142
\endcode
143
144
\section1 string Date::fromLocaleString(locale, dateTimeString, format)
145
146
Converts the datetime string \a dateTimeString to a \l {The Date JavaScript Object}{Date}
147
object using \a locale and \a format.
148
149
If \a format is not specified, \l {QtQml::Locale}{Locale.LongFormat} will
150
be used.
151
152
If \a locale is not specified, the default locale will be used.
153
154
The following example shows a datetime being parsed from a datetime string
155
in a certain format using the default locale:
156
\code
157
import QtQml 2.0
158
159
QtObject {
160
property var locale: Qt.locale()
161
property string dateTimeString: "Tue 2013-09-17 10:56:06"
162
163
Component.onCompleted: {
164
print(Date.fromLocaleString(locale, dateTimeString, "ddd yyyy-MM-dd hh:mm:ss"));
165
}
166
}
167
\endcode
168
169
\section1 string Date::fromLocaleDateString(locale, dateString, format)
170
171
Converts the date string \a dateString to a \l {The Date JavaScript Object}{Date} object
172
using \a locale and \a format.
173
174
If \a format is not specified, \l {QtQml::Locale}{Locale.LongFormat} will
175
be used.
176
177
If \a locale is not specified, the default locale will be used.
178
179
The following example shows the current date first being formatted as a date
180
string using the default locale and format, then parsed back again in the
181
same manner:
182
\code
183
import QtQml 2.0
184
185
QtObject {
186
property var locale: Qt.locale()
187
property date currentDate: new Date()
188
property string dateString
189
190
Component.onCompleted: {
191
dateString = currentDate.toLocaleDateString();
192
print(Date.fromLocaleDateString(dateString));
193
}
194
}
195
\endcode
196
197
\section1 string Date::fromLocaleTimeString(locale, timeString, format)
198
199
Converts the time string \a timeString to a \l {The Date JavaScript Object}{Date} object
200
using \a locale and \a format.
201
202
If \a format is not specified, \l {QtQml::Locale}{Locale.LongFormat} will
203
be used.
204
205
If \a locale is not specified, the default locale will be used.
206
207
The following example shows the current time first being formatted as a time
208
string using the default locale and a short format, then parsed back again
209
in the same manner:
210
\code
211
import QtQml 2.2
212
213
QtObject {
214
property var locale: Qt.locale()
215
property date currentTime: new Date()
216
property string timeString
217
218
Component.onCompleted: {
219
timeString = currentTime.toLocaleTimeString(locale, Locale.ShortFormat);
220
print(Date.fromLocaleTimeString(locale, timeString, Locale.ShortFormat));
221
}
222
}
223
\endcode
224
225
\section1 string Date::timeZoneUpdated()
226
227
Informs the JS engine that the system's timezone has been changed, which is necessary
228
for the correct manipulation of datetime data.
229
230
JS stores Date objects in UTC time; all access to and from Date components in local
231
time involves the application of the current offset from UTC. If the current offset
232
changes due to the timezone being updated, the JS engine needs to be informed so that
233
it can recalculate the offset.
234
235
This function should be called after the system's timezone has been updated.
236
237
For example, an application that changes the timezone would call timeZoneUpdated() after
238
setting the new time zone:
239
240
\code
241
property string selectedTimeZone
242
243
onSelectedTimeZoneChanged: {
244
MyFunctions.setSystemTimeZone(selectedTimeZone)
245
Date.timeZoneUpdated()
246
}
247
\endcode
248
*/
qtdeclarative
src
qml
doc
src
javascript
date.qdoc
Generated on
for Qt by
1.16.1