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
date -u +%Y-%m-%dT%H:%M:%SZPortable 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`.
date -u +%Y-%m-%dT%H:%M:%SZdate -u +%Y-%m-%dT%H:%M:%SZ(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.
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
- Get the current timestamp— Print the current date / time as either a Unix epoch (seconds since 1970-01-01 UTC) or an ISO 8601 string — for log lines, filenames, expiry calculations, and HTTP `Date` headers.
- Get the current time in UTC— Print the current wall-clock time in UTC (not local time) — essential for log correlation across machines and any DST-safe scheduling logic.
- Convert a Unix epoch to a human-readable date— Render a Unix-epoch integer (e.g. `1747396980`) as a readable date — for log inspection, file-metadata audits, and translating database timestamps.
- Parse an ISO 8601 date string— Take a string like `2026-05-16T14:23:00Z` and convert it to a typed date object — for filtering logs by time, computing age, or feeding into date arithmetic.