Skip to content

DateTime

We can use time package to create a DateTime object. Time for create a time object.

Usage

nvs

Class Methods

new

Create a new DateTime object.

nvs

parse (time)

Parse a string to a DateTime object, default time format is RFC3339.

If the time format is invalid, it will return nil.

If there not timezone, use UTC as default.

nvs

Or it compibility to supports %Y-%m-%d %H:%M:%S format.

nvs

parse (time, format)

You can use parse to pass 2 arguments, the first is the time string, the second is the time format.

nvs

More details of the time format, see Time Format.

Instance Methods

year

Return the year of the time.

nvs

month

Return the month of the time.

nvs

day

Return the day of the time.

nvs

hour

Return the hour of the time.

nvs

minute

Return the minute of the time.

nvs

second

Return the second of the time.

nvs

timestamp

Return the Unix Timestamp (in second) of the time.

nvs

format

Return the time string with the Time Format format.

nvs

iso8601

alias: to_string

Return the time string with the RFC3339 format.

nvs

to_string

alias: iso8601

Return the time string with the RFC3339 format.

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.