Linux commands
Browse topics

Formatting and calculating dates with date

Version
unversioned
Review
Draft

Summary#

date prints a time in a chosen format. GNU date can also parse a time you give it with -d.

Syntax#

sh
date +%F
date -u +%Y-%m-%dT%H:%M:%SZ
date +%s
date -d '@1700000000'
date -d '2026-01-15 +30 days' +%F
TZ=Asia/Ho_Chi_Minh date

+ starts a format: %F is year, month and day, and %s is seconds since 1970. -u uses UTC and -d parses the given date. TZ selects a time zone for one command.

Example#

sh
(
  set -eu
  date -u -d '@0' +%Y-%m-%dT%H:%M:%SZ
  date -u -d '2026-01-15 +30 days' +%F
  date -u -d '2026-03-01T00:00:00Z' +%s
)

Expected output#

Text
1970-01-01T00:00:00Z
2026-02-14
1772323200

Common mistakes#

%m is the month and %M is the minute. %Y is the calendar year; %G is the ISO week year and differs around New Year. Local-time arithmetic can cross a daylight saving change, so calculate in UTC. Use date -u +%Y%m%dT%H%M%SZ in file names to sort correctly and avoid colons. On BSD and macOS, -d has another meaning; they use -r for timestamps and -v for adjustments.

Version applicability#

This entry describes date from GNU Coreutils 9; the example also matched Rust Coreutils 0.10.

Sources#

Attribution
DevLookup contributors
License
CC-BY-4.0