Date QML Type

提供日期函数。更多

Import Statement: import QtQml

方法

详细说明

QML Date(日期)对象扩展了JS Date(日期)对象,并提供了本地化功能。

接受format 参数的函数接受 Locale.LongFormat、Locale.ShortFormat、Locale.NarrowFormat 枚举值,或指定格式的字符串。

支持的格式字符串的形式如QDate::toString(),QTime::toString() 和QDateTime::toString() 文档中所述。

如果日期无效,则返回空字符串。

格式枚举值

当您需要与本地首选项相匹配的格式时,请使用枚举值。

Locale.LongFormat(长格式较长格式
Locale.ShortFormat 短格式较短格式
Locale.NarrowFormat在此情况下与 Locale.ShortFormat 相同

枚举所代表的格式取决于您的本地语言,也取决于枚举所使用的方法。

例如,在en_US 本地语言中,将使用这些格式字符串:

功能本地枚举格式字符串
fromLocaleDateString,toLocaleDateStringLocale.LongFormatdddd, MMMM d, yyyy
fromLocaleDateString,toLocaleDateStringLocale.ShortFormatM/d/yy
fromLocaleTimeString,toLocaleTimeString本地长格式h:mm:ss AP t
fromLocaleTimeString,toLocaleTimeString短格式h:mm AP
fromLocaleString,toLocaleString本地长格式dddd, MMMM d, yyyy h:mm:ss AP t
fromLocaleString,toLocaleStringLocale.ShortFormatM/d/yy h:mm AP

其他注意事项

由于 Qt 和 JS 的规范不一致,使用本地感知函数执行日期或时间格式化可能会导致时间格式不正确。ECMA-262 规定,应通过将当前的夏令时规则投射到过去几年来解释历史日期,而 Qt 则使用历史数据(如果可用)来确定给定日期是否实行夏令时。因此,在 JS 中构建一个 Date 值并使用本地感知函数将其转换为字符串时,如果当前实行夏令时,则结果可能与指定时间不一致,差一个小时,反之亦然。

不同的日期格式对负年份有不同的理解。普通人类语言中没有 0 年,1BC 之后的年份是 1AD。在打印或解析未被 ECMAScript 标准化的格式之一的日期时,就会反映出这种理解。即:toString()、toLocaleString()、toUTCString() 和朋友们。ECMAScript 标准化了一种格式:ISO 8601。这就是调用 toISOString() 时得到的格式。这种格式包含一个年份 0,在其他格式中为 1BC。因此,在使用 toISOString() 和 toString() 打印负日期时,会得到不同的年份。

在使用 Date 构造函数或 set(UTC)FullYear() 设置年份时,使用的是 ISO 8601 规定的惯例,0 是有效年份。这意味着使用构造函数或 set(UTC)FullYear() 设置的负年份以 0 为基础,因此与 toString() 和好友函数打印的年份相差一年。解析任何 to*String()方法的输出结果都将与打印的日期值相同。Date.parse() 将识别不同的格式及其关于 0 年存在的约定。

请注意,这一切都与其他 JavaScript 实现不同,后者通常将所有字符串表示中的年份 0 都视为有效。在 ECMAScript 标准中,日期格式是 "取决于实现 "的,因此这仍然有效。

另请参见 Locale

方法文档

string fromLocaleDateString(locale, dateString, format)

使用localeformat 将日期字符串dateString 转换为Date 对象。

如果未指定format ,将使用Locale.LongFormat

如果未指定locale ,将使用默认语言。

下面的示例显示了使用默认语言和格式首先将当前日期格式化为日期字符串,然后再以同样的方式解析回来:

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)

使用localeformat 将日期时间字符串dateTimeString 转换为Date 对象。

如果未指定format ,将使用Locale.LongFormat

如果未指定locale ,将使用默认语言。

下面的示例显示了使用默认 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)

使用localeformat 将时间字符串timeString 转换为Date 对象。

如果未指定format ,将使用Locale.LongFormat

如果未指定locale ,则将使用默认 locale。

下面的示例显示了使用默认语言和简短格式将当前时间首先格式化为时间字符串,然后再以同样的方式解析回来:

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()

通知 JS 引擎系统时区已更改,这是正确处理日期时间数据所必需的。

JS 以 UTC 时间存储日期对象;以本地时间对日期组件进行的所有访问和从日期组件进行的访问都涉及应用当前偏移量(UTC)。如果当前偏移量因时区更新而发生变化,则需要通知 JS 引擎,以便其重新计算偏移量。

该函数应在系统时区更新后调用。

例如,更改时区的应用程序会在设置新时区后调用 timeZoneUpdated():

property string selectedTimeZone

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

string toLocaleDateString(locale, format)

将日期转换为包含适合指定locale 的日期字符串,该字符串位于指定的format 中。

如果未指定format ,则使用Locale.LongFormat

如果未指定locale ,则将使用默认地域。

下面的示例显示了格式化为德语语言的当前日期:

import QtQuick 2.0

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

string toLocaleString(locale, format)

在指定的format 中将日期转换为包含适合指定locale 的日期和时间的字符串。

如果未指定format ,将使用Locale.LongFormat

如果未指定locale ,将使用默认语言。

下面的示例显示了当前日期和时间在德语地区的格式化:

import QtQuick 2.0

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

string toLocaleTimeString(locale, format)

在指定的format 中将日期转换为包含适合指定locale 的时间的字符串。

如果未指定format ,将使用Locale.LongFormat

如果未指定locale ,则将使用默认语言。

下面的示例显示了格式化为德语语言的当前时间:

import QtQuick 2.0

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

© 2025 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.