Skip to main content
formatdate(format, time) formats time using the given format string. time must be an RFC 3339 timestamp string.

Signature

formatdate(format string, time string) string

Format tokens

TokenDescriptionExample
YYYYFour-digit year2024
YYTwo-digit year24
MMTwo-digit month01-12
MMMThree-letter month abbreviationJan
MMMMFull month nameJanuary
DDTwo-digit day01-31
DDay without leading zero1-31
hhTwo-digit hour (24-hour)00-23
mmTwo-digit minute00-59
ssTwo-digit second00-59
ZZZTimezone abbreviationUTC
ZTimezone offset+00:00

Example

Stamp a release artifact with a timestamp passed in from the environment or CI system:
variable "version" {
  type = string
}

variable "build_timestamp" {
  description = "RFC 3339 build timestamp (e.g. 2024-03-15T10:30:00Z)"
  type        = string
}

computed "release_date" {
  description = "Date portion of the build timestamp"
  expression  = formatdate("YYYY-MM-DD", var.build_timestamp)
}

task "package" {
  description = "Build a dated release archive"
  commands = [
    "go build -o bin/app ./cmd/app",
    "tar -czf releases/app-${var.version}-${computed.release_date}.tar.gz bin/app",
  ]
}
Pass the timestamp at run time:
errand run package --var version=1.4.2 --var build_timestamp=2024-03-15T10:30:00Z
The archive name becomes app-1.4.2-2024-03-15.tar.gz.

Notes

  • time must be a valid RFC 3339 string (e.g. 2024-03-15T10:30:00Z). An invalid format produces an error at evaluation time.
  • formatdate does not perform timezone conversion. The output timezone depends on the input timestamp.