我正在尝试使用在 Qml 中的 Date 类型上实现的 toLocaleDateString / toLocateTimeString 方法显示日期(和时间)(此处的文档)。它可以工作,除了与 NarrowFormat 相同的 ShortFormat。
这是一个例子:
import QtQuick 2.3
import QtQuick.Controls 1.2
ApplicationWindow {
visible: true
width: 640
height: 480
property var locale: Qt.locale()
Label {
id: date
text: {
"DATE: \n" +
"Locale.LongFormat:\t" + new Date().toLocaleDateString(locale, Locale.LongFormat) + "\n" +
"Locale.ShortFormat:\t" + new Date().toLocaleDateString(locale, Locale.ShortFormat) + "\n" +
"Locale.NarrowFormat:\t" + new Date().toLocaleDateString(locale, Locale.NarrowFormat) + "\n"
}
anchors.top : parent
}
Label {
id: time
text:
"TIME: \n" +
"Locale.LongFormat:\t" + new Date().toLocaleTimeString(locale, Locale.LongFormat) + "\n" +
"Locale.ShortFormat:\t" + new Date().toLocaleTimeString(locale, Locale.ShortFormat) + "\n" +
"Locale.NarrowFormat:\t" + new Date().toLocaleTimeString(locale, Locale.NarrowFormat) + "\n"
anchors.top: date.bottom
}
}
显示的内容:
Locale.LongFormat: Wednesday, September 27, 2017
Locale.ShortFormat: 9/27/17
Locale.NarrowFormat: 9/27/17
Locale.LongFormat: 11:46:10 AM CEST
Locale.ShortFormat: 11:46 AM
Locale.NarrowFormat: 11:46 AM
我期待 ShotFormat 给我的日期是 2017 年 9 月 27 日或类似的日期,时间是上午 11:46:42。
我在文档(here)中找到了关于 NarrowFormat 的内容,即
此外,对于系统语言环境,此格式与 ShortFormat 相同。
我不确定这是否与我的问题有关,因为我在 toLocaleDateMethod 中明确给出了语言环境。即使那样,NarrowFormat 应该更详细,而不是 ShortFormat 更短......
谢谢你的帮助 !