Skip to content

日期时间

我们可以使用 time 包来创建一个 DateTime 对象。

使用

nvs

类方法

new

创建一个新的 DateTime 对象。

nvs

parse (time)

解析一个字符串为 DateTime 对象,默认时间格式为 RFC3339。 如果时间格式无效,将返回 nil。 如果字符串内没有包含时区信息,将会默认采用 UTC 时区。

nvs

或者也兼容支持 %Y-%m-%d %H:%M:%S 格式。

nvs

parse (time, format)

你可以采用 parse 方法传递两个参数,第一个是时间字符串,第二个是时间格式。

nvs

更多细节的时间格式,参见 Time Format

实例方法

year

返回时间的年份。

nvs

month

返回月份。

nvs

day

返回时间的日期。

nvs

hour

返回时间的小时。

nvs

minute

返回时间的分钟。

nvs

second

返回时间的秒数。

nvs

timestamp

返回时间的 Unix 时间戳(秒)格式的数字。

nvs

format

基于参数的 Time Format 返回时间字符串。

nvs

iso8601

alias: to_string

返回时间字符串,格式为 RFC3339

nvs

to_string

alias: iso8601

返回时间字符串,格式为 RFC3339

Time Format

CodeExampleDescription
%aSunWeekday as locale’s abbreviated name.
%ASundayWeekday as locale’s full name.
%w0Weekday as a decimal number, where 0 is Sunday and 6 is Saturday.
%d08Day of the month as a zero-padded decimal number.
%bSepMonth as locale’s abbreviated name.
%BSeptemberMonth as locale’s full name.
%m09Month as a zero-padded decimal number.
%y13Year without century as a zero-padded decimal number.
%Y2013Year with century as a decimal number.
%H07Hour (24-hour clock) as a zero-padded decimal number.
%I07Hour (12-hour clock) as a zero-padded decimal number.
%pAMLocale’s equivalent of either AM or PM.
%M06Minute as a zero-padded decimal number.
%S05Second as a zero-padded decimal number.
%f000000Microsecond as a decimal number, zero-padded on the left.
%z+0000UTC offset in the form ±HHMM[SS[.ffffff]] (empty string if the object is naive).
%ZUTCTime zone name (empty string if the object is naive).
%j251Day of the year as a zero-padded decimal number.
%U36Week number of the year (Sunday as the first day of the week) as a zero padded decimal number. All days in a new year preceding the first Sunday are considered to be in week 0.
%W35Week number of the year (Monday as the first day of the week) as a decimal number. All days in a new year preceding the first Monday are considered to be in week 0.
%cSun Sep 8 07:06:05 2013Locale’s appropriate date and time representation.
%x09/08/13Locale’s appropriate date representation.
%X07:06:05Locale’s appropriate time representation.
%%%A literal % character.