datetime
The datetime module provides utilities to work with current date and time, and to format or parse date strings. It is written in Koji: std/datetime.kj.
Functions
now() -> DateTime
Returns the current date and time. When printed or interpolated, it is shown as a formatted string (e.g.2025-04-20 14:30:00), by itsdisplaymethod; inside a list or another value, its fields are shown, as for any type.A
DateTimehas these fields (allint):year: full yearmonth: month (1–12)day: day of the month (1–31)hour: hour (0–23)minute: minutessecond: seconds
today() -> string
Returns the current date as a string (e.g.2025-04-20).format(timestamp: decimal, pattern: string) -> string
Converts a timestamp (seconds since the epoch; anintis adecimaltoo, a fraction of a second is dropped) to a date string that followspattern.Format tokens (case-insensitive, e.g.
YYYY-MM-DDoryyyy-mm-dd) can include:yyyy: full yearmm: month (01–12)dd: day of the month (01–31)hh: hour (00–23)min: minutes (00–59)ss: seconds (00–59)
parse(text: string, pattern: string) -> Result[int, DateError]
Converts a date string that followspatterninto a timestamp (seconds since the epoch). The pattern uses the same tokens asformat.type DateError = { PatternMismatch(text: string, pattern: string) // the text does not follow the pattern InvalidDate(text: string) // e.g. 2025-02-30 }
Dates and times are in UTC. A timestamp counts the seconds since 0000-01-01 00:00:00 UTC, the epoch of Koji (see time): format(0, 'yyyy-mm-dd') is 0000-01-01, and a timestamp of Unix plus 62167219200 is one of Koji.
Example
import datetime
immut date = datetime.today()
print(date)
match (datetime.parse('2025-04-20', 'YYYY-MM-DD')) {
Ok(timestamp) => print(datetime.format(timestamp, 'DD/MM/YYYY')) // 20/04/2025
Err(error) => print(error)
}
immut now = datetime.now()
print(now.year)