The DateTime class represents a specific point in time. DateTime instances are immutable.
- class DateTime()
- Construct a DateTime object representing the current time and date in local time.
- class DateTime(year as Int, month as Int, day as Int, hour as Int, minute as Int, second as Int)
- class DateTime(year as Int, month as Int, day as Int, hour as Int, minute as Int, second as Float)
- Construct a DateTime object representing a specific point in time. Seconds must be a float or an integer and all other arguments must be integers. None of the values are restricted to any specific range. If they are outside the valid range of values, they are normalized and the excess is moved to the larger units. So if seconds is 64, the actual second value becomes 4 and minutes are incremented by one, etc.
- class DateTime(string as Str[, format as Str])
Construct a DateTime object from a string. The format argument
specifies the format of the string. If the format is omitted, the format
will be "YYYY-MM-DD hh:mm:ss.s".
The format must contain format sequences for all DateTime components (seconds, minutes, hours, day, month, year). A format sequence matches a DateTime component in the string. The following list contains all the available format sequences and examples of valid values:
s seconds (1, 30) ss seconds, at least 2 digits (01, 30) s.s seconds with an optional fraction (5, 5.0, 5.12, ...) ss.s seconds with an optional fraction, at least 2 digits in the integer part (05, 05.0, 05.12, ...) m minutes (1, 30) mm minutes, at least 2 digits (01, 30) h hours (1, 13) hh hours, at least 2 digits (01, 30) [am/pm] specifier for a.m. or p.m. (all h and hh sequences in the same format string will also follow 12-hour clock conventions; "am" and "pm" in the formatting sequence may be replaced with arbitrary strings, and the matched string must contain one of these strings) D day of month (1, 2, ..., 31) DD day of month, at least 2 digits (01, 02, ... 31) M month (1, 2, ..., 12) MM month, at least 2 digits (01, 02, ... 12) MMM abbreviated name of month (Jan, Feb, ..., Dec) MMMM name of month (January, February, ..., December) YY year, 2 digits (00, 01, ... 99) YYYY year, 4 digits (2005)
Case is not significant in the month names and abbreviations. The YY values 69-99 are mapped to years 1969-1999, and 00-68 are mapped to years 2000-2068. It is recommended to use the YYYY format for years so that years before 1969 and after 2068 can be represented.
Any other characters in the format must be matched by the same character in the string. As an exception, a backslash \ followed by a character c must be matched by c.
- year as Int
- Year of the date
- month as Int
- Month of the date (1 = January, 2 = February, ...)
- day as Int
- Day of month (the first day of the month is 1)
- hour as Int
- Hour (range 0 to 23, inclusive)
- minute as Int
- Minute (range 0 to 59, inclusive)
- second as Float
- Seconds (0 <= second < 60; float)
- weekday as Int
- Weekday of the date (1 = Monday, 2 = Tuesday, ..., 7 = Sunday).
- week([rule as Constant]) as Int
- Return the week number. The rule argument specifies how the week number is calculated. It can be one of FirstWeek, FirstFourDayWeek or FirstFullWeek. If unspecified, FirstFourDayWeek is assumed. Sunday is considered to be the last day of the week and Monday the first.
- format(template as Str) as Str
Format the date and time as a string. The template string defines the format of the result. The following formatting sequences in the template are replaced with formatted DateTime components (examples within parentheses):
s seconds as an integer (5) ss seconds as an integer, at least 2 digits (05) s.s, s.ss, ... seconds as a fraction (5.0, 5.12, ...) ss.s, ss.ss, ... seconds as a fraction, at least 2 digits in the integer part (05.0, 05.12, ...) h hours (6) hh hours, at least 2 digits (06) m minutes (4) mm minutes, at least 2 digits (04) d days (3) dd, ddd, ... days, at least n digits (03, 003, ...) - sign of the time (empty or "-") [am/pm] a.m. or p.m. according 12-hour clock; all h and hh sequences in the same format string will also follow 12-hour clock conventions ("am" and "pm" may be replaced with arbitrary strings, and one of these is substituted in the result string) D day of month (5) DD day of month, at least 2 digits (05) M month (5) MM month, at least 2 digits (05) MMM abbreviated name of month (Feb) MMMM name of month (February) YY year, 2 digits (06) YYYY year, 4 digits (2006) WWW abbreviation of weekday (Wed) WWWW weekday (Wednesday)
Other characters in the template are copied as such to the result. As an exception, a backslash \ followed by a character c is replaced with c.
- datetime + time (DateTime + Time ⇒ DateTime)
- datetime - time (DateTime - Time ⇒ DateTime)
- Time objects can be added to and subtracted from DateTime objects.
- Str(datetime) (Str(DateTime) ⇒ Str)
- Convert a DateTime object to a string of the form "YYYY-MM-DD hh:mm:ss".
- datetime1 == datetime2 (DateTime == DateTime ⇒ Boolean)
- datetime1 < datetime2 (DateTime < DateTime ⇒ Boolean)
- DateTime objects can be compared for equality and order. An earlier instant is smaller than a later instant.
- Hash(datetime) (Hash(DateTime) ⇒ Int)
- Return the hash value of a DateTime object.