date
An instant — a specific moment in time — is a number: milliseconds since 1970, the same value JavaScript's Date.now() gives. Do math on it directly with the usual operators and duration literals: now() + 2h, deadline - now(). A calendar date — a whole day, like "today" — stays a "YYYY-MM-DD" string, because which day it is depends on the timezone. Use format to turn an instant into a readable string, formatDate for its calendar date, and parse to read an ISO string back into an instant.
import { now, atTime, nextDayOfWeek, format } from "std::date"
import { createEvent } from "std::calendar"
node main() {
// 3pm next Monday Pacific for one hour
const tz = "America/Los_Angeles"
const start = atTime(nextDayOfWeek("monday", tz), "15:00", tz)
createEvent(summary: "Dentist", start: format(start, tz), end: format(start + 1h, tz))
}Types
DayOfWeek
A day of the week, lowercase.
/** A day of the week, lowercase. */
export type DayOfWeek =
| "sunday"
| "monday"
| "tuesday"
| "wednesday"
| "thursday"
| "friday"
| "saturday"(source)
Functions
now
now(): numberGet the current time as milliseconds since the Unix epoch.
Use format(now(), timezone) to display the instant in a timezone. The numeric instant itself identifies the same moment in every timezone.
Returns: number
(source)
today
today(timezone: string = ""): stringGet today's date as a YYYY-MM-DD string (e.g. "2026-05-05").
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
| timezone | string | "" | IANA timezone name (defaults to the local timezone) |
Returns: string
(source)
tomorrow
tomorrow(timezone: string = ""): stringGet tomorrow's date as a YYYY-MM-DD string (e.g. "2026-05-06").
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
| timezone | string | "" | IANA timezone name (defaults to the local timezone) |
Returns: string
(source)
nextDayOfWeek
nextDayOfWeek(day: DayOfWeek, timezone: string = ""): stringGet the next occurrence of a weekday as a YYYY-MM-DD date.
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
| day | DayOfWeek | ||
| timezone | string | "" | IANA timezone name, or empty for the local timezone. |
Returns: string
(source)
atTime
atTime(date: string, time: string, timezone: string = ""): numberConvert a calendar date and local time to epoch milliseconds.
Use atTime("2026-05-05", "09:00", "America/New_York") to schedule a local wall-clock time. Invalid dates and times throw an error.
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
| date | string | Calendar date in YYYY-MM-DD form. | |
| time | string | Wall-clock time in HH:MM or HH:MM:SS form. | |
| timezone | string | "" | IANA timezone name, or empty for the local timezone. |
Returns: number
(source)
startOfDay
startOfDay(instant: number | null = null, timezone: string = ""): numberGet midnight of the day containing the instant, as epoch milliseconds.
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
| instant | number | null | null | Epoch milliseconds within the day, or omit for now. |
| timezone | string | "" | IANA timezone name, or empty for the local timezone. |
Returns: number
(source)
endOfDay
endOfDay(instant: number | null = null, timezone: string = ""): numberGet the last millisecond of the day containing the instant.
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
| instant | number | null | null | Epoch milliseconds within the day, or omit for now. |
| timezone | string | "" | IANA timezone name, or empty for the local timezone. |
Returns: number
(source)
startOfWeek
startOfWeek(instant: number | null = null, timezone: string = ""): numberGet midnight on Sunday of the week containing the instant, as epoch milliseconds.
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
| instant | number | null | null | Epoch milliseconds within the week, or omit for now. |
| timezone | string | "" | IANA timezone name, or empty for the local timezone. |
Returns: number
(source)
endOfWeek
endOfWeek(instant: number | null = null, timezone: string = ""): numberGet the last millisecond of Saturday of the week containing the instant.
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
| instant | number | null | null | Epoch milliseconds within the week, or omit for now. |
| timezone | string | "" | IANA timezone name, or empty for the local timezone. |
Returns: number
(source)
startOfMonth
startOfMonth(instant: number | null = null, timezone: string = ""): numberGet midnight on the first day of the month containing the instant, as epoch milliseconds.
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
| instant | number | null | null | Epoch milliseconds within the month, or omit for now. |
| timezone | string | "" | IANA timezone name, or empty for the local timezone. |
Returns: number
(source)
endOfMonth
endOfMonth(instant: number | null = null, timezone: string = ""): numberGet the last millisecond of the month containing the instant.
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
| instant | number | null | null | Epoch milliseconds within the month, or omit for now. |
| timezone | string | "" | IANA timezone name, or empty for the local timezone. |
Returns: number
(source)
format
format(ms: number, timezone: string = ""): stringFormat an instant (epoch milliseconds) as an ISO 8601 string with milliseconds and offset, e.g. "2026-05-05T10:30:00.123-07:00".
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
| ms | number | The instant to format | |
| timezone | string | "" | IANA timezone name (defaults to local) |
Returns: string
(source)
formatDate
formatDate(ms: number, timezone: string = ""): stringFormat an instant as the "YYYY-MM-DD" calendar date it falls on in a timezone.
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
| ms | number | The instant to format | |
| timezone | string | "" | IANA timezone name (defaults to local) |
Returns: string
(source)
parse
parse(iso: string): numberParse a datetime string into epoch milliseconds.
Parsing follows JavaScript Date rules, so inputs such as "2026" and "2026-05" are also accepted. Invalid input throws an error.
Parameters:
| Name | Type | Default |
|---|---|---|
| iso | string |
Returns: number
(source)
formatDuration
formatDuration(ms: number): stringFormat a duration in milliseconds as text such as "5m 32s".
Output uses whole seconds, with weeks as the largest unit.
Parameters:
| Name | Type | Default |
|---|---|---|
| ms | number |
Returns: string
(source)
elapsedTime
elapsedTime(since: number): stringFormat the time since an earlier instant as a duration such as "5m 32s".
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
| since | number | Starting time in epoch milliseconds, such as a value from now(). |
Returns: string
(source)