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