Skip to content

time

dates and times: read them from text, do arithmetic with them, and show them in plain words

Dates and times. A date is a moment: a day and a time of day in a time zone. A duration is a length of time.

Durations are quantities with a time unit, like 90 min, and units can run together: 1h30m.

Dates typed without a zone are in your computer’s local time. today, tomorrow and yesterday are midnight at the start of those days. Months and years follow the calendar, so adding a month to January 31 gives the last day of February.

Tags: time

  • date a date from text: ISO (2026-12-25), US (12/25/2026), written out (Dec 25 2026) or plain English (next friday at 5pm, 3 days ago)
  • year the year of a date
  • month the month of a date, from 1 for January to 12
  • day the day of the month, from 1 to 31
  • hour the hour of a date, from 0 to 23
  • minute the minute of a date, from 0 to 59
  • second the second of a date, from 0 to 59
  • weekday the name of the day of the week, like “Friday”
  • format a date as text laid out by strftime codes, such as %Y for the year, %m the month, %d the day, %H:%M the time and %B the month’s name
  • with the same date with some parts changed, given as a map of year, month, day, hour, minute and second, like {day: 1}
  • is_weekend whether the date is a Saturday or Sunday
  • is_weekday whether the date is Monday to Friday
  • is_today whether the date falls on today’s date
  • is_past whether the date is earlier than now
  • is_future whether the date is later than now
  • age how many whole years have passed since d, as for someone’s age from their birthday
  • diff the time from a to b in calendar terms
  • relative how far the date is from now, in words like “in 3 days” or “2 hours ago”
  • parts a duration split into the largest units that fit, up to three of days, hours, minutes and seconds
  • hms a duration as clock text, like 7:15:00, with hours that can pass 24
  • unix the Unix timestamp: seconds since the start of 1970 in UTC, the same as d to unix
  • unix_ms the Unix timestamp in milliseconds, the same as d to unix_ms
  • date_ms the date for a Unix timestamp in milliseconds, the form JavaScript and Java use
  • timeit how long it takes to call f, which takes no arguments
  • stopwatch the time since stopwatch was last called (0 s the first time), for timing parts of a script
1
# date: a moment, made with date() or now
2
date("2026-12-25 18:30")2026-12-25 18:30:00 -06:00
3
# time of day: 9:30 means 9:30 today
4
9:30 + 45 min2026-11-04 10:15:00 -06:00
5
# duration: an amount of time
6
1h30m90 min
Example outputCtrl+Enter to run
1
# date + or - a duration moves it
2
date("2026-01-31") + 1 mo2026-02-28
3
# date - date is the duration between
4
date("2027-01-01") - date("2026-12-25")7 d
5
# subtracting two times of day in quotes gives hours
6
"17:00" - "08:45"8.25 h
7
# < and > compare which is earlier
8
yesterday < nowtrue
9
# to unix: seconds since 1970 (UTC)
10
date("2026-12-25") to unix1798178400
11
# to unix_ms does the same in milliseconds. Unix timestamps are how many systems store times
12
# to date reads a date from text
13
"2026-12-25" to date2026-12-25
Example outputCtrl+Enter to run
1
# a date, long: written out, time included
2
pretty(date("2026-12-25"))"Friday, December 25, 2026 at 12:00 AM"
3
# a date, short
4
pretty(date("2026-12-25 18:30"), "short")"Dec 25, 2026, 6:30 PM"
5
# a date not in local time names its zone
6
pretty(date("2026-12-25 18:30") to "Asia/Tokyo")"Saturday, December 26, 2026 at 9:30 AM JST"
7
# a duration, long: up to 3 of d, h, min and s
8
pretty(5000 s)"1 h 23 min 20 s"
9
# a duration, short: up to 2 of them
10
pretty(5000 s, "short")"1 h 23 min"
Example outputCtrl+Enter to run
date(s: str) -> date / date(s: str, fmt: str) -> date / date(y: int, m: int, d: int, h: int = 0, min: int = 0, s: int = 0) -> date / date(y: int, m: int, d: int, calendar: str) -> date / date(unix: int) -> date

a date from text: ISO (2026-12-25), US (12/25/2026), written out (Dec 25 2026) or plain English (next friday at 5pm, 3 days ago)

Also from a format string with strftime codes, from year, month, day and optional time numbers, from year, month and day in another calendar (“hebrew”, “islamic”, “persian”, “julian”, “coptic”, “ethiopian” or “indian”), or from a Unix timestamp

1
date("2026-12-25")2026-12-25
2
date("next friday at 5pm")2026-11-06 17:00:00 -06:00
3
date("3 days ago")2026-11-01 12:12:40 -06:00
4
date("25.12.2026", "%d.%m.%Y")2026-12-25
5
date(2026, 12, 25, 18, 30)2026-12-25 18:30:00 -06:00
6
date(5787, 7, 1, "hebrew")2026-09-12
7
date(1405, 1, 1, "persian")2026-03-21
8
date(0)1969-12-31 18:00:00 -06:00
Example outputCtrl+Enter to run

See also format, with, start_of

year(d: date) -> int

the year of a date

1
now.year2026
Example outputCtrl+Enter to run

See also month, day

month(d: date) -> int

the month of a date, from 1 for January to 12

1
now.month11
Example outputCtrl+Enter to run

See also year, day

day(d: date) -> int

the day of the month, from 1 to 31

1
now.day4
Example outputCtrl+Enter to run

See also month, weekday

hour(d: date) -> int

the hour of a date, from 0 to 23

1
now.hour12
Example outputCtrl+Enter to run

See also minute, second

minute(d: date) -> int

the minute of a date, from 0 to 59

1
now.minute12
Example outputCtrl+Enter to run

See also hour, second

second(d: date) -> int

the second of a date, from 0 to 59

1
now.second40
Example outputCtrl+Enter to run

See also hour, minute

weekday(d: date) -> str

the name of the day of the week, like “Friday”

1
date("2026-12-25").weekday"Friday"
Example outputCtrl+Enter to run

See also day, format

format(d: date, fmt: str) -> str / format(fmt: str, *args: any) -> str

a date as text laid out by strftime codes, such as %Y for the year, %m the month, %d the day, %H:%M the time and %B the month’s name

Given a format string first instead, formats numbers printf-style

1
now.format("%B %d, %Y")"November 04, 2026"
2
now.format("%H:%M")"12:12"
3
format("%5.2f%%", 12.345)"12.35%"
Example outputCtrl+Enter to run

Tags: formatting

See also date, fixed

with(d: date, fields: map) -> date

the same date with some parts changed, given as a map of year, month, day, hour, minute and second, like {day: 1}

1
today.with({day: 1})2026-11-01
2
now.with({hour: 9, minute: 0})2026-11-04 09:00:40 -06:00
Example outputCtrl+Enter to run

See also start_of, date

is_weekend(d: date) -> bool

whether the date is a Saturday or Sunday

1
date("2026-10-10").is_weekendtrue
Example outputCtrl+Enter to run

See also is_weekday, add_workdays

is_weekday(d: date) -> bool

whether the date is Monday to Friday

1
today.is_weekdaytrue
Example outputCtrl+Enter to run

See also is_weekend

is_today(d: date) -> bool

whether the date falls on today’s date

1
now.is_todaytrue
2
tomorrow.is_todayfalse
Example outputCtrl+Enter to run

See also is_past

is_past(d: date) -> bool

whether the date is earlier than now

1
yesterday.is_pasttrue
Example outputCtrl+Enter to run

See also is_future, is_today

is_future(d: date) -> bool

whether the date is later than now

1
tomorrow.is_futuretrue
Example outputCtrl+Enter to run

See also is_past

age(d: date) -> int

how many whole years have passed since d, as for someone’s age from their birthday

1
date("1990-06-15").age36
Example outputCtrl+Enter to run

See also diff

diff(a: date, b: date) -> str

the time from a to b in calendar terms

Years, months, days and smaller

1
diff(date("2025-08-03"), date("2026-10-06 04:00"))"1 yr 2 mo 3 d 4 h"
Example outputCtrl+Enter to run

See also age, relative, parts

relative(d: date) -> str

how far the date is from now, in words like “in 3 days” or “2 hours ago”

1
date("2026-12-25").relative"in 2 months"
2
(now - 3 h).relative"3 hours ago"
Example outputCtrl+Enter to run

See also diff, parts

parts(duration: quantity) -> str

a duration split into the largest units that fit, up to three of days, hours, minutes and seconds

For units of your choice, write to h min

1
(date("2026-12-25") - date("2026-10-06 14:24")).parts"79 d 10 h 36 min"
2
5000 s.parts"1 h 23 min 20 s"
Example outputCtrl+Enter to run

See also relative, diff, hms

hms(duration: quantity) -> str

a duration as clock text, like 7:15:00, with hours that can pass 24

Shows milliseconds when there are any

1
7.25 h to hms"7:15:00"
2
(90 min).hms"1:30:00"
3
5000.5 s.hms"1:23:20.500"
Example outputCtrl+Enter to run

See also parts

unix(d: date) -> int

the Unix timestamp: seconds since the start of 1970 in UTC, the same as d to unix

1
date(0).unix0
2
date(86400) to unix86400
Example outputCtrl+Enter to run

See also date, unix_ms

unix_ms(d: date) -> int

the Unix timestamp in milliseconds, the same as d to unix_ms

1
date(1).unix_ms1000
Example outputCtrl+Enter to run

See also date_ms, unix

date_ms(ms: int) -> date

the date for a Unix timestamp in milliseconds, the form JavaScript and Java use

1
date_ms(1798178400000) == date(1798178400)true
Example outputCtrl+Enter to run

See also unix_ms, date

timeit(f: fn) -> quantity

how long it takes to call f, which takes no arguments

1
timeit(|| (1..1000).sum) < 1 strue
Example outputCtrl+Enter to run

See also stopwatch

stopwatch() -> quantity

the time since stopwatch was last called (0 s the first time), for timing parts of a script

1
stopwatch() >= 0 strue
Example outputCtrl+Enter to run

See also timeit

1
# day of the week
2
date("2026-12-25").weekday"Friday"
3
# adding a month stops at the end of a shorter month
4
date("2026-01-31") + 1 mo2026-02-28
5
# plain-English dates
6
date("next friday")2026-11-06
7
# how long until new year
8
(date("2027-01-01") - now).parts"57 d 11 h 47 min"
9
# 3 hours ago, in words
10
(now - 3 h).relative"3 hours ago"
11
# first of next month
12
(today + 1 mo).with({day: 1})2026-12-01
13
# months with a Friday the 13th
14
(1..=12).filter(|m| date(2026, m, 13).weekday == "Friday")[2, 3, 11]
15
# a Unix timestamp as a date
16
date(1798178400)2026-12-25
17
# durations written without spaces
18
1h30m + 2d4h3210 min
19
# hours worked from 8:45 to 17:00
20
17:00 - 8:458.25 h
Example outputCtrl+Enter to run

Example results generated on 2026-11-04. Ones using now, today or randomness will differ when you run them.