QML类型说明-Date

来源:互联网 发布:java base64 byte数组 编辑:程序博客网 时间:2024/06/08 17:51

Date

ImportStatement:   import QtQml 2.2

 

Methods

stringfromLocaleDateString(locale, dateString, format)

stringfromLocaleString(locale, dateTimeString, format)

stringfromLocaleTimeString(locale, timeString, format)

stringtimeZoneUpdated()

stringtoLocaleDateString(locale, format)

stringtoLocaleString(locale, format)

stringtoLocaleTimeString(locale, format)

 

DetailedDescription

QML的Date对象是JS Date对象的本地化扩展。它能接受枚举行的本地化的格式:

Locale.LongFormat      字符的长版本,例如"January"作为1月。

Locale.ShortFormat      字符的短版本,例如"Jan"作为1月

Locale.NarrowFormat   指定的狭窄格式。例如"J"作为1月。注意这种格式下,不同的月和天可能有同样的文本。如果系统不支持,甚至是空文本。所以我们尽量避免使用它。另外,这种格式的本地语言环境,相当于ShortFormat。

组件也能接受用来表达日期的指定文本格式。

表达式

输出

d

没有前导零的天(1到31)

dd

有前导零的天(01到31)

ddd

压缩的本地化天('Mon' 到'Sun')

dddd

完全的本地化天 ('Monday' 到'Sunday')

M

没有前导零的月 (1到12)

MM

有前导零的月(01到12)

MMM

压缩的本地化月('Jan' 到'Dec').

MMMM

月的本地化全名('January'到'December').

yy

两个数字表示年(00到99)

yyyy

四个数字表示年,如果念在公园前,附加负号为前缀。

所有其他输入字符被忽略。被单引号封闭的任何字符序列,被视作文本而不是表达式。连续两个单引号在输出时被一个单引号替代。

例如格式字符处理1969年7月20日是下面这样的。

格式

结果

dd.MM.yyyy

20.07.1969

ddd MMMM d yy

Sun July 20 69

'The day is' dddd

The day is Sunday

下面的表达式用在时间上:

表达式

输出

h

没有前导零的小时(0到23或1到12)

hh

有前导零的小时(00到23或01到12)

H

没有前导零的小时 (0到23,即使分AM/PM显示)

HH

有前导零的小时(00到23,即使分AM/PM显示)

m

没有前导零的分钟(0到59)

mm

有前导零的分钟(00到59)

s

没有前导零的秒(0到59)

ss

有前导零的秒(00到59)

z

没有前导零的毫秒(0到999)

zzz

有前导零的毫秒(000到999)

AP or A

用AM/PM显示,AP将被放置为"AM"或"PM"

ap or a

用am/pm显示,ap将被放置为"am"或"pm"

t

时区(例如"CEST")

所有其他输入字符被忽略。被单引号封闭的任何字符序列,被视作文本而不是表达式。连续两个单引号在输出时被一个单引号替代。

例如格式字符处理14:13:09.042是下面这样的:

格式

结果

hh:mm:ss.zzz

14:13:09.042

h:m:s ap

2:13:9 pm

H:m:s a

14:13:9 pm

如果日期是无效的,将返回空字符。注意空字符值一个字符都没有的QString,而不是空指针。

注意使用本地化功能处理日期和时间,可能产生不正确的时间格式。这是因为Qt和JS的规范不一致导致的。ECMA-262指定历史日期将按照当前的规则,依据夏令法解释。QT用历史数据(如果有)去确定夏令对给定的日期的影响,因此从JS获得的当前日期转换成本地数据,如果DST(夏令)没有被指定,它将产生印象,让当前结果产生1个小时的误差。反之亦然。

 

MethodDocumentation

stringfromLocaleDateString(locale, dateString, format)

使用locale(语言)和format(格式)转换dataString(日期字符串)到Date对象。

如果格式没有指定,使用Locale.LongFormat。如果语言没有指定,使用默认语言。下面的例子展示了从Date对象获取本地格式的日期,然后再转回去打印出来。

import QtQml2.0

 

QtObject {

    property var locale: Qt.locale()

    property date currentDate: new Date()

    property string dateString

 

    Component.onCompleted: {

        dateString =currentDate.toLocaleDateString();

       print(Date.fromLocaleDateString(dateString));

    }

}

 

stringfromLocaleString(locale, dateTimeString, format)

使用locale(语言)和format(格式)转换dateTimeString(日期时间字符串)到Date对象。

如果格式没有指定,使用Locale.LongFormat。如果语言没有指定,使用默认语言。下面的例子展示了将本地格式的日期时间转换成Date后打印。

import QtQml2.0

 

QtObject {

    property var locale: Qt.locale()

    property string dateTimeString: "Tue2013-09-17 10:56:06"

 

    Component.onCompleted: {

        print(Date.fromLocaleString(locale,dateTimeString, "ddd yyyy-MM-dd hh:mm:ss"));

    }

}

 

stringfromLocaleTimeString(locale, timeString, format)

使用locale(语言)和format(格式)转换timeString(时间字符串)到Date对象。

如果格式没有指定,使用Locale.LongFormat。如果语言没有指定,使用默认语言。下面的例子展示了从Date对象获取本地格式的时间,然后再转回去打印出来。

import QtQml2.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));

    }

}

 

stringtimeZoneUpdated()

通知JS引擎本地时区已经改变,为了正确处理datetime数据,这是必须的。

JS保存UTC时间的Date对象。所有使用本地环境访问Date组件(包括设置和读取)的操作,都需要从UTC抵消时区偏移量。如果时区更新,时区偏移当然也要更新。

这个函数应该在系统时区变化后被调用。

示例:

propertystring selectedTimeZone

 

onSelectedTimeZoneChanged:{

   MyFunctions.setSystemTimeZone(selectedTimeZone)

    Date.timeZoneUpdated()

}

 

stringtoLocaleDateString(locale, format)

使用locale和format转换Date对象为本地化的日期字符串。

如果格式没有指定,使用Locale.LongFormat。如果语言没有指定,使用默认语言。下面的例子展示了将当前日期转换成德国环境的日期。

importQtQuick 2.0

 

Text {

    text: "The date is: " + Date().toLocaleDateString(Qt.locale("de_DE"))

}

 

stringtoLocaleString(locale, format)

使用locale和format转换Date对象为本地化的日期时间字符串。

如果格式没有指定,使用Locale.LongFormat。如果语言没有指定,使用默认语言。下面的例子展示了将当前日期和时间转换成德国环境的日期和时间。

importQtQuick 2.0

 

Text {

    text: "The date is: " + Date().toLocaleString(Qt.locale("de_DE"))

}

 

stringtoLocaleTimeString(locale, format)

使用locale和format转换Date对象为本地化的时间字符串。

如果格式没有指定,使用Locale.LongFormat。如果语言没有指定,使用默认语言。下面的例子展示了将当前时间转换成德国环境的时间。

importQtQuick 2.0

 

Text {

    text: "The date is: " + Date().toLocaleTimeString(Qt.locale("de_DE"))

}
0 0
原创粉丝点击