Contents

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 its display method; inside a list or another value, its fields are shown, as for any type.

    A DateTime has these fields (all int):

    • year: full year
    • month: month (1–12)
    • day: day of the month (1–31)
    • hour: hour (0–23)
    • minute: minutes
    • second: 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; an int is a decimal too, a fraction of a second is dropped) to a date string that follows pattern.

    Format tokens (case-insensitive, e.g. YYYY-MM-DD or yyyy-mm-dd) can include:

    • yyyy: full year
    • mm: 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 follows pattern into a timestamp (seconds since the epoch). The pattern uses the same tokens as format.

    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)

Edit this page on GitHub