Skip to navigation

Date and time functions

All timestamps are RFC 3339 strings you supply. These functions never read the current clock.

FunctionResult and rules
formatdate(pattern, timestamp)Format a timestamp with the tokens below.
timeadd(timestamp, duration)Add a signed duration; returns RFC 3339 with whole-second formatting.
timecmp(left, right)Compare instants; -1 if left is earlier, 0 if equal, 1 if later.

timeadd accepts signed combinations of h, m, s, ms, us, µs, μs, and ns, including fractional units and bare 0. Duration arithmetic is bounded to signed 64-bit nanoseconds; nonrepresentable timestamps fail. Output formatting omits subsecond precision. Resource timeout and schedule fields use their own duration rules.

TokenMeaning
YYYY, YYFour-/two-digit year.
MMMM, MMM, MM, MFull/short month name, padded and unpadded month.
DD, DPadded/unpadded day.
EEEE, EEEFull/short weekday name.
hh, hPadded/unpadded 24-hour hour.
HH, HPadded/unpadded 12-hour hour.
mm, m, ss, sPadded/unpadded minute or second.
AA, aaAM/PM in upper-/lowercase.
ZZZZZ, ZColon-separated offset; Z renders Z at UTC.
ZZZZ, ZZZCompact offset; ZZZ renders UTC at UTC.

Quote literal alphabetic text with single quotes; doubled single quotes emit a quote. Unknown tokens fail. Clock/random functions such as timestamp and plantimestamp are unavailable.

Example

locals {
times = {
date = formatdate("YYYY-MM-DD", "2026-01-01T00:00:00Z")
later = timeadd("2026-01-01T00:00:00Z", "1h30m")
order = timecmp("2026-01-01T00:00:00Z", "2026-01-02T00:00:00Z")
}
}
output "times" {
type = string
value = jsonencode(local.times)
}

All calls obey the function limits and annotation rules.