0

我正在尝试使用在 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 更短......

谢谢你的帮助 !

4

1 回答 1

0

在 Qml 文档中:

Locale.ShortFormat是默认和推荐的格式,因为Locale.NarrowFormat每个语言环境可能不完全支持(请参阅语言环境字符串格式类型),并且Locale.LongFormat可能不适合标题单元格。

于 2020-02-19T21:44:11.740 回答