Skip to content

Casts an expression to a time value.

time(x:any) -> time

The time function parses the given string x to a time value. It automatically recognizes many common timestamp formats without requiring a format string.

The function accepts timestamps in the following formats:

The general form is YYYY-MM-DD, optionally followed by a time component:

ComponentFormatExample
DateYYYY-MM-DD2024-01-15
Year and monthYYYY-MM2024-01
Date and hourYYYY-MM-DD⊔HH2024-01-15T10
Date and minutesYYYY-MM-DD⊔HH:MM2024-01-15T10:30
Full datetimeYYYY-MM-DD⊔HH:MM:SS2024-01-15T10:30:45
With fractionsYYYY-MM-DD⊔HH:MM:SS.fff2024-01-15T10:30:45.123456
With timezoneYYYY-MM-DD⊔HH:MM:SS⊔TZ2024-01-15T10:30:45+02:00

The date-time separator can be T, a space, or +. Missing time components default to zero.

Timestamps can end with a timezone offset:

FormatExample
UTCZ
With colon+02:00, -05:30
Without+0200, -0530
Hour only+02, -05

Prefix a Unix epoch value with @:

FormatExample
Seconds@1705316445
Fractional@1705316445.123
FormatExample
Current timenow
Future offsetnow + 1h, in 2d
Past offsetnow - 30min, 5min ago

For timestamps in non-standard formats, use parse_time with an explicit format string.

from {
date_only: time("2024-01-15"),
with_time: time("2024-01-15T10:30:45"),
with_space: time("2024-01-15 10:30:45"),
fractional: time("2024-01-15T10:30:45.123456"),
}
{
date_only: 2024-01-15T00:00:00Z,
with_time: 2024-01-15T10:30:45Z,
with_space: 2024-01-15T10:30:45Z,
fractional: 2024-01-15T10:30:45.123456Z,
}

Missing components default to their minimum value:

from {
year_month: time("2024-01"),
date_hour: time("2024-01-15T10"),
date_minute: time("2024-01-15T10:30"),
}
{
year_month: 2024-01-01T00:00:00Z,
date_hour: 2024-01-15T10:00:00Z,
date_minute: 2024-01-15T10:30:00Z,
}
from {
utc: time("2024-01-15T10:30:45Z"),
with_colon: time("2024-01-15T10:30:45+02:00"),
without_colon: time("2024-01-15T10:30:45+0200"),
hour_only: time("2024-01-15T10:30:45-05"),
}
{
utc: 2024-01-15T10:30:45Z,
with_colon: 2024-01-15T08:30:45Z,
without_colon: 2024-01-15T08:30:45Z,
hour_only: 2024-01-15T15:30:45Z,
}

Prefix Unix timestamps with @:

from {
unix_seconds: time("@1705316445"),
unix_fractional: time("@1705316445.123456"),
}
{
unix_seconds: 2024-01-15T11:00:45Z,
unix_fractional: 2024-01-15T11:00:45.123456Z,
}
from {
current: time("now"),
future: time("now + 2h"),
past: time("now - 30min"),
in_future: time("in 1d"),
in_past: time("5min ago"),
}
from {timestamp: "2024-01-15T10:30:45Z"}
timestamp = timestamp.time()
{
timestamp: 2024-01-15T10:30:45Z,
}

Last updated: