Skip to content

Time ​

Timestamps and durations. Calendar functions take an optional IANA time zone as their last argument and default to UTC. The examples on this page run with the clock fixed at 2026-01-15T10:30:00.000Z, so now() returns that instant.

Every function can be called as f(x, ...) or as a method, x.f(...). In the signatures, T and U stand for any type, T[] is a list, ? marks an optional parameter, and (T, number) => boolean is a lambda parameter.

FunctionDescription
nowThe current time, fixed for one evaluation.
timestampParses ISO-8601 text or epoch milliseconds into a timestamp.
durationParses ISO-8601 duration text in weeks, days, hours, minutes, and seconds (e.g. "PT1H30M", "-P2DT0.5S"), the text a duration renders as.
weeksA duration of n weeks.
daysA duration of n days (24 hours each).
hoursA duration of n hours.
minutesA duration of n minutes.
secondsA duration of n seconds.
millisecondsA duration of n milliseconds. Durations are whole milliseconds: a fraction rounds to the nearest one, halves away from zero.
inDaysA duration as a (fractional) number of days.
inHoursA duration as a number of hours.
inMinutesA duration as a number of minutes.
inSecondsA duration as a number of seconds.
inMillisecondsA duration as a number of milliseconds.
yearThe calendar year, in a time zone (default UTC).
monthThe month 1-12, in a time zone (default UTC).
dayThe day of the month, in a time zone (default UTC).
hourThe hour 0-23, in a time zone (default UTC).
minuteThe minute, in a time zone (default UTC).
secondThe second, in a time zone (default UTC).
dayOfWeekThe ISO weekday (Monday 1 to Sunday 7), in a time zone (default UTC).
startOfDayMidnight at the start of the day, in a time zone (default UTC).
startOfMonthMidnight on the first of the month, in a time zone (default UTC).
startOfYearMidnight on January 1, in a time zone (default UTC).
addDaysAdds calendar days (keeps the wall-clock time across DST), in a time zone.
addMonthsAdds calendar months, clamping the day to the month length.
addYearsAdds calendar years (Feb 29 becomes Feb 28).
formatDateFormats a timestamp in a time zone (default UTC). Tokens: yyyy yy MMMM MMM MM M dd d EEEE EEE HH H hh h a mm m ss s SSS; quote literal text.
absAbsolute value.
sumThe sum of the numbers in a list (nulls are skipped), added left to right as a + b + c is.

now ​

The current time, fixed for one evaluation.

  • now(): timestamp
bonsai
now() // => 2026-01-15T10:30:00.000Z

timestamp ​

Parses ISO-8601 text or epoch milliseconds into a timestamp.

  • timestamp(string): timestamp
  • timestamp(number): timestamp
  • timestamp(timestamp): timestamp
bonsai
timestamp("2026-03-01T12:00:00Z") // => 2026-03-01T12:00:00.000Z
timestamp("2026-03-01") // => 2026-03-01T00:00:00.000Z
timestamp("2026-03-01T12:00:00+02:00") // => 2026-03-01T10:00:00.000Z
timestamp(0) // => 1970-01-01T00:00:00.000Z

duration ​

Parses ISO-8601 duration text in weeks, days, hours, minutes, and seconds (e.g. "PT1H30M", "-P2DT0.5S"), the text a duration renders as.

  • duration(string): duration
  • duration(duration): duration
bonsai
duration("PT1H30M") // => PT1H30M
duration("-P2DT0.5S") // => -P2DT0.5S
duration("P1W") == weeks(1) // => true

weeks ​

A duration of n weeks.

  • weeks(number): duration
bonsai
weeks(2) // => P14D

days ​

A duration of n days (24 hours each).

  • days(number): duration
bonsai
days(3) // => P3D
now() + days(30) // => 2026-02-14T10:30:00.000Z

hours ​

A duration of n hours.

  • hours(number): duration
bonsai
hours(1.5) // => PT1H30M

minutes ​

A duration of n minutes.

  • minutes(number): duration
bonsai
minutes(90) // => PT1H30M

seconds ​

A duration of n seconds.

  • seconds(number): duration
bonsai
seconds(0.5) // => PT0.5S

milliseconds ​

A duration of n milliseconds. Durations are whole milliseconds: a fraction rounds to the nearest one, halves away from zero.

  • milliseconds(number): duration
bonsai
milliseconds(1500) // => PT1.5S
milliseconds(0.5) == milliseconds(1) // => true

inDays ​

A duration as a (fractional) number of days.

  • inDays(duration): number
bonsai
inDays(hours(36)) // => 1.5
inDays(now() - timestamp("2026-01-01T00:00:00Z")) // => 14.4375

inHours ​

A duration as a number of hours.

  • inHours(duration): number
bonsai
days(2).inHours() // => 48

inMinutes ​

A duration as a number of minutes.

  • inMinutes(duration): number
bonsai
hours(1).inMinutes() // => 60

inSeconds ​

A duration as a number of seconds.

  • inSeconds(duration): number
bonsai
minutes(2).inSeconds() // => 120

inMilliseconds ​

A duration as a number of milliseconds.

  • inMilliseconds(duration): number
bonsai
seconds(1).inMilliseconds() // => 1000

year ​

The calendar year, in a time zone (default UTC).

  • year(timestamp, string?): number
bonsai
year(now()) // => 2026

month ​

The month 1-12, in a time zone (default UTC).

  • month(timestamp, string?): number
bonsai
month(now()) // => 1

day ​

The day of the month, in a time zone (default UTC).

  • day(timestamp, string?): number
bonsai
day(now()) // => 15

hour ​

The hour 0-23, in a time zone (default UTC).

  • hour(timestamp, string?): number
bonsai
hour(now()) // => 10
hour(now(), "America/New_York") // => 5
now().hour("Asia/Tokyo") // => 19

minute ​

The minute, in a time zone (default UTC).

  • minute(timestamp, string?): number
bonsai
minute(now()) // => 30

second ​

The second, in a time zone (default UTC).

  • second(timestamp, string?): number
bonsai
second(now()) // => 0

dayOfWeek ​

The ISO weekday (Monday 1 to Sunday 7), in a time zone (default UTC).

  • dayOfWeek(timestamp, string?): number
bonsai
dayOfWeek(now()) // => 4

startOfDay ​

Midnight at the start of the day, in a time zone (default UTC).

  • startOfDay(timestamp, string?): timestamp
bonsai
startOfDay(now()) // => 2026-01-15T00:00:00.000Z
startOfDay(now(), "Europe/Berlin") // => 2026-01-14T23:00:00.000Z

startOfMonth ​

Midnight on the first of the month, in a time zone (default UTC).

  • startOfMonth(timestamp, string?): timestamp
bonsai
startOfMonth(now()) // => 2026-01-01T00:00:00.000Z

startOfYear ​

Midnight on January 1, in a time zone (default UTC).

  • startOfYear(timestamp, string?): timestamp
bonsai
startOfYear(now()) // => 2026-01-01T00:00:00.000Z

addDays ​

Adds calendar days (keeps the wall-clock time across DST), in a time zone.

  • addDays(timestamp, number, string?): timestamp
bonsai
addDays(timestamp("2026-03-28T12:00:00Z"), 2, "Europe/Berlin") // => 2026-03-30T11:00:00.000Z
timestamp("2026-03-28T12:00:00Z") + days(2) // => 2026-03-30T12:00:00.000Z

addMonths ​

Adds calendar months, clamping the day to the month length.

  • addMonths(timestamp, number, string?): timestamp
bonsai
addMonths(timestamp("2026-01-31T00:00:00Z"), 1) // => 2026-02-28T00:00:00.000Z

addYears ​

Adds calendar years (Feb 29 becomes Feb 28).

  • addYears(timestamp, number, string?): timestamp
bonsai
addYears(timestamp("2024-02-29T00:00:00Z"), 1) // => 2025-02-28T00:00:00.000Z

formatDate ​

Formats a timestamp in a time zone (default UTC). Tokens: yyyy yy MMMM MMM MM M dd d EEEE EEE HH H hh h a mm m ss s SSS; quote literal text.

  • formatDate(timestamp, string, string?): string
bonsai
formatDate(now(), "yyyy-MM-dd HH:mm") // => "2026-01-15 10:30"
now().formatDate("dd/MM/yyyy HH:mm", "Asia/Tokyo") // => "15/01/2026 19:30"

abs ​

Absolute value.

  • abs(duration): duration
bonsai
abs(hours(-2)) // => PT2H

sum ​

The sum of the numbers in a list (nulls are skipped), added left to right as a + b + c is.

  • sum((duration | null)[]): duration
bonsai
[hours(1), minutes(30)].sum() // => PT1H30M