Skip to content
shellmap

Format the current date as ISO 8601

Emit `YYYY-MM-DDTHH:MM:SS±HH:MM` (or the UTC `…Z` form) — the format every machine-readable log, JSON API, and database column should agree on.

How to format the current date as iso 8601 in each shell

Bashunix
date -u +%Y-%m-%dT%H:%M:%SZ

Portable across GNU + BSD/macOS. GNU-only shortcut: `date -Iseconds` (prints local zone with `+HH:MM` offset). For UTC with offset notation: `date -u -Iseconds` → `2026-05-16T14:23:00+00:00`.

Zshunix
date -u +%Y-%m-%dT%H:%M:%SZ
Fishunix
date -u +%Y-%m-%dT%H:%M:%SZ
PowerShellwindows
(Get-Date).ToUniversalTime().ToString("o")

pwsh `"o"` (round-trip / ISO 8601) outputs `2026-05-16T14:23:00.0000000Z`. For seconds-precision: `.ToString("yyyy-MM-ddTHH:mm:ssZ")`. Don't use `-UFormat %s` style formatting — it's a lossy POSIX wrapper.

cmd.exewindows
powershell -NoProfile -Command "(Get-Date).ToUniversalTime().ToString('o')"

cmd has NO ISO 8601 primitive. `%date%` returns locale-dependent `"Sat 05/16/2026"` (US) vs `"16/05/2026"` (UK) — useless for sortable timestamps. Shell out to pwsh.

Equivalents listed for Bash, Zsh, Fish, PowerShell, cmd.exe.

Gotchas & notes

  • ISO 8601 has three valid output forms: **basic** (`20260516T142300Z` — no separators, used in tar/zip filename conventions); **extended** (`2026-05-16T14:23:00Z` — human-readable, what JSON / RFC 3339 / SQL TIMESTAMP all use); **extended with fractional seconds** (`2026-05-16T14:23:00.123Z`). RFC 3339 (the JSON-friendly subset) requires `Z` or `±HH:MM` — never bare local time. The `T` separator may be a literal space per RFC 3339 §5.6 but most parsers prefer `T`.
  • GNU `date -Iseconds` is the cleanest one-liner but **does not exist on BSD/macOS** — `date -Iseconds` errors out. The portable expression is `date -u +"%Y-%m-%dT%H:%M:%SZ"`. macOS BSD `date` accepts the same `%Y`/`%m`/`%d`/`%H`/`%M`/`%S` strftime specifiers as GNU; the divergence is only in convenience flags (`-I`, `-R`, `--rfc-3339`). For local-zone with offset on macOS: `date +"%Y-%m-%dT%H:%M:%S%z"` then post-process `%z` (`+0200`) → `+02:00` with `sed 's/\([+-][0-9][0-9]\)\([0-9][0-9]\)$/\1:\2/'`.
  • pwsh `(Get-Date).ToString("o")` returns `2026-05-16T14:23:00.1234567+02:00` (LOCAL zone with offset — round-trip-safe via `[DateTime]::Parse`). For UTC `Z` form, call `.ToUniversalTime()` first. The `[DateTimeOffset]` type preserves timezone correctly across serialization; `[DateTime]` does NOT (it has a `.Kind` field but many APIs strip it). For new code in pwsh 7+ prefer `[DateTimeOffset]::UtcNow.ToString("yyyy-MM-ddTHH:mm:ssZ")` — explicit UTC, no ambiguity.
  • fish has no built-in date arithmetic or formatting — it shells out to `date` for every operation. The single-quoted format string requires no special escaping in fish (`date +%Y-%m-%dT%H:%M:%SZ` works as-is). For deterministic timestamps in scripts (avoiding mid-execution drift), capture once: `set ts (date -u +%Y-%m-%dT%H:%M:%SZ); echo "log at $ts"`. cmd is the worst-case — `%date%` and `%time%` are locale-formatted, NOT ISO; parsing them requires `for /f "tokens=1-4 delims=/. " %a in ("%date%")` and even then you don't know the order (MM/DD vs DD/MM). Shell out to pwsh for any cmd ISO need.

Related commands

Related tasks