radlang — Dates & Time (Date)

This document covers the built-in Date type: creating dates, reading their components, formatting, and parsing.

For language fundamentals see the Overview.


Overview

Date is a built-in value type — no import needed. Internally it holds a single millisecond timestamp (epoch, UTC), but you work with it through constructors and methods. All component getters (getHours, getDate, …) report values in local time.

fn main() {
    Date now = Date()                      // current date/time
    sys.output(now.toString())             // e.g. Wed Jul 08 2026 14:32:01
    sys.output(now.getFullYear())          // 2026
}

Creating a date

Date()                 : Date     // current date/time
Date.new()             : Date     // current date/time (alias of Date())
Date(ms: long)         : Date     // from an epoch-millisecond timestamp
Date(text: string)     : Date     // parse a date string
Date.now()             : long     // current time as raw epoch ms (NOT a Date)
Date.now.micro()       : long     // current time as raw epoch microseconds
Date.now.nano()        : long     // current time as raw epoch nanoseconds
  • Date() / Date.new() build a Date for the current moment.
  • Date(ms) builds a Date from a millisecond timestamp — round-trips with getTime().
  • Date(text) parses a string (see Parsing).
  • Date.now() returns the current time as a bare long (epoch ms), handy for timing/deltas without allocating a Date.
  • Date.now.micro() / Date.now.nano() return the current time as a bare long in microseconds / nanoseconds. On macOS Date.now.nano() has only microsecond precision (the last three digits are always 000), and the compiler warns when it sees the call.
fn main() {
    Date a = Date()                        // now
    Date b = Date(0)                       // the epoch: 1970-01-01T00:00:00 UTC
    Date c = Date("2026-05-17")            // parsed
    long  t = Date.now()                   // e.g. 1783705921000
    long  us = Date.now.micro()            // e.g. 1783705921000234
    long  nanos = Date.now.nano()          // e.g. 1783705921000234000 (`ns` is a reserved word)

    sys.output(b.getTime())                // 0
    sys.output(c.getFullYear())            // 2026
}

Note. Date(ms) stores a UTC instant, but the component getters below report local time. So Date(0).getFullYear() is 1970 only in UTC/ahead timezones — west of UTC it reads 1969 (epoch 0 is 1969-12-31 locally). Use getTime() when you need the absolute, timezone-independent value.


Reading components

Each getter returns an int (except getTime, a long), in local time.

Method Returns Range / notes
getTime() long Epoch milliseconds
getFullYear() int Four-digit year, e.g. 2026
getMonth() int 0–11 (0 = January)
getDate() int Day of month, 1–31
getDay() int Day of week, 0–6 (0 = Sunday)
getHours() int 0–23
getMinutes() int 0–59
getSeconds() int 0–59
fn main() {
    Date d = Date("2026-05-17 09:30:00")
    sys.output(d.getFullYear())   // 2026
    sys.output(d.getMonth())      // 4   (May — zero-based!)
    sys.output(d.getDate())       // 17
    sys.output(d.getHours())      // 9
    sys.output(d.getMinutes())    // 30
}

Each getter has a UTC twin that ignores the host timezone: getUTCFullYear(), getUTCMonth(), getUTCDate(), getUTCDay(), getUTCHours(), getUTCMinutes() and getUTCSeconds() (same ranges, all int).

Note. getMonth() is zero-based (January = 0, December = 11) and getDay() is the weekday starting at Sunday = 0 — matching JavaScript’s Date. Add 1 to getMonth() for a human month number.


Formatting

Default string form

date.toString()             : string
date.toLocaleDateString()   : string

Both render the date with the built-in format Www Mmm DD YYYY HH:MM:SS (e.g. Sun May 17 2026 09:30:00). This form round-trips: Date(d.toString()) parses back to the same instant.

fn main() {
    Date d = Date()
    sys.output(d.toString())    // Wed Jul 08 2026 14:32:01
}

ISO 8601 string

date.toISOString()          : string

Renders the instant in UTC as YYYY-MM-DDTHH:MM:SS.mmmZ, whatever the host timezone. Date(d.toISOString()) parses back to the same instant.

fn main() {
    Date d = Date("2026-05-17T09:30:00+02:00")
    sys.output(d.toISOString())   // 2026-05-17T07:30:00.000Z
}

Custom format

date.format(template: string) : string

Substitutes these tokens in template (everything else is copied verbatim):

Token Meaning Example
YYYY 4-digit year 2026
MM 2-digit month (1-based) 05
dd 2-digit day of month 17
HH 2-digit hour (24h) 09
mm 2-digit minute 30
ss 2-digit second 00
zzz 3-digit milliseconds 042
fn main() {
    Date d = Date("2026-05-17 09:30:00")
    sys.output(d.format("YYYY-MM-dd"))            // 2026-05-17
    sys.output(d.format("dd/MM/YYYY HH:mm:ss"))   // 17/05/2026 09:30:00
    sys.output(d.format("YYYY-MM-ddTHH:mm:ss.zzz")) // 2026-05-17T09:30:00.000
}

Note. In format templates, MM is the 1-based month (unlike the zero-based getMonth()), and case matters: MM = month, mm = minute, HH = hour, ss = second, dd = day.


Arithmetic and comparison

date.diff(other: Date)      : long     // this - other, in milliseconds
date.addMs(n: long)         : Date
date.addSeconds(n: int)     : Date
date.addMinutes(n: int)     : Date
date.addHours(n: int)       : Date
date.addDays(n: int)        : Date
date.addMonths(n: int)      : Date
date.addYears(n: int)       : Date

The add methods return a new Date; the receiver is not changed. A negative count goes back in time. addMs, addSeconds, addMinutes, addHours and addDays add an exact number of milliseconds (a day is always 24 hours, even across a daylight-saving change). addMonths and addYears are calendar-aware and work in local time, but a day that does not exist in the target month overflows into the next one instead of clamping: January 31 plus one month is March 3 (March 2 in a leap year).

fn main() {
    Date d = Date("2026-05-17 09:30:00")
    sys.output(d.addDays(1).format("YYYY-MM-dd"))                  // 2026-05-18
    sys.output(d.addMonths(1).format("YYYY-MM-dd"))                // 2026-06-17
    sys.output(Date("2026-01-31").addMonths(1).format("YYYY-MM-dd")) // 2026-03-03
    sys.output(d.diff(Date("2026-05-17 09:00:00")))                // 1800000
}

== compares two dates by instant. < and > are not defined on Date (a compile error): compare getTime() values, or use diff.


Parsing

Date(text) accepts these formats, tried in order:

  • Www Mmm DD YYYY HH:MM:SS — the toString() output (so it round-trips)
  • YYYY-MM-DDTHH:MM:SS — ISO 8601 datetime; optional fractional seconds and Z or ±HH:MM / ±HHMM offset
  • YYYY-MM-DD HH:MM:SS — space-separated datetime
  • YYYY-MM-DD — bare ISO date (time defaults to local midnight)

Explicit zones are independent of the host timezone. Fractions are preserved to millisecond precision (extra digits truncate). Unzoned inputs retain local-time semantics. Unrecognized strings or malformed suffixes throw a catchable Error (invalid date string: 'garbage'). Day and month numbers are not range checked against the calendar: Date("2026-02-30") is March 2, 2026.

fn main() {
    Date d1 = Date("2026-05-17T09:30:00")
    Date d2 = Date("2026-05-17")            // 09:30 dropped → local midnight
    sys.output(d1.getHours())               // 9
    sys.output(d2.getHours())               // 0
}

Timing example

fn main() {
    long start = Date.now()
    sys.sleep(50)
    long elapsed = Date.now() - start
    sys.output("waited ~" + elapsed.toString() + " ms")
}

Quick reference

Call Returns Purpose
Date() / Date.new() Date Current date/time
Date(ms) Date From epoch milliseconds
Date(text) Date Parse a date string
Date.now() long Current time as epoch ms
Date.now.micro() long Current time as epoch microseconds
Date.now.nano() long Current time as epoch nanoseconds
date.getTime() long Epoch milliseconds
date.getFullYear() int Year
date.getMonth() int Month, 0–11
date.getDate() int Day of month
date.getDay() int Weekday, 0–6 (Sun=0)
date.getHours/getMinutes/getSeconds() int Time components
date.toString() / toLocaleDateString() string Default formatted string
date.format(tmpl) string Custom-formatted string
date.toISOString() string UTC ISO 8601 string
date.getUTCFullYear() … getUTCSeconds() int UTC components
date.diff(other) long Milliseconds between two dates
date.addMs/addSeconds/addMinutes/addHours/addDays/addMonths/addYears(n) Date A shifted copy

Picking the right tool

  • A wall-clock value you’ll read fields from → Date().
  • Just measuring elapsed time → Date.now() (raw long, no allocation).
  • Storing/transmitting a timestamp → getTime() (a long you can rebuild with Date(ms)).
  • Human-readable output → toString(), or format(...) for a specific layout.