shell · difficulty ◆◆
date — format, convert and compare time
One instant, four clocks — and the only format you should ever store is the ugly integer.
Your log says 09:03, your colleague in Riyadh says 11:03, and the certificate says it expired yesterday — same second, three different stories.
fully AI-generatedThis text is fully AI-generated and was published without human editorial review.$ dateWhat it does
date does three jobs behind one name: it prints the clock (formatted by whatever %-escapes you give it), it parses a date string into a timestamp (-d, or -f for a whole file of them), and it can set the clock (-s, root only). -u switches the display to UTC, -r reads a file's mtime instead of the current time, and TZ=Europe/Vienna in front of the command changes the timezone for that one process. In scripts the only output that matters is %s — seconds since 1970-01-01T00:00:00Z — because it is a plain integer you can subtract, sort and compare without any timezone reasoning at all.
Why it matters
Almost every sysadmin question has a timestamp hidden in it. How long was the API down? Do these two log lines bracket the deploy? Does the TLS certificate outlive the weekend? How old is that lock file? Answering any of them means getting several clock readings into one comparable form, and on a mixed fleet you are reading a container in UTC, a database in local time, an access log with a +0100 offset and a colleague's message that says 'just after nine'. date -d plus %s is the whole conversion toolkit, no spreadsheet involved.
How I actually use it
I format for humans and compute in %s, never the other way round. When something looks wrong I put TZ= in front of the command rather than touching /etc/localtime, so a one-off check never leaves a container in a different timezone than the image expects. And I assume the box is lying about 'now' until I have seen both date and date -u agree with timedatectl — a clock that drifted by four minutes during a suspend is enough to make TLS handshakes and TOTP codes fail in ways that look like application bugs.
Example
$ date
date -u
date +%s
TZ=Asia/Riyadh dateWed Oct 28 09:03:28 AM CET 2026
Wed Oct 28 08:03:28 AM UTC 2026
1793174608
Wed Oct 28 11:03:28 AM +03 2026Four renderings of one instant, captured seconds apart on the same box. CET here, UTC on the container, +03 for the client in Yanbu, and 1793174608 as the only line you can compare without thinking. Note that -u changes the display, not the clock: the epoch printed by all four is identical.
$ date -d @1793400000
date -u -d @1793400000 +%FT%TZ
date -d @0Fri Oct 30 11:40:00 PM CET 2026
2026-10-30T22:40:00Z
Thu Jan 1 01:00:00 AM CET 1970The @ prefix is how you hand date an epoch. First line is local time, second is the same number forced to UTC and ISO 8601 — that pair is what I paste into tickets. The third line is the giveaway that the epoch is absolute and the clock face is not: zero seconds lands on 01:00 here, because this box is one hour ahead of UTC.
$ TZ=Europe/Vienna date -d '2026-03-29 02:30'
TZ=Europe/Vienna date -d '2026-03-29 03:30'
TZ=Europe/Vienna date -d '2026-10-25 02:30' +%zdate: invalid date ‘2026-03-29 02:30’
Sun Mar 29 03:30:00 AM CEST 2026
+0100On the last Sunday of March the clocks jump from 02:00 to 03:00, so 02:30 that night never happens — and GNU date says so instead of inventing an answer. Six months later 02:30 happens twice, and date picks the first one, CET (+0100). This is why a 02:30 cron entry is the classic spring-forward casualty: there is no 02:30 for it to fire at.
$ date '+%Y-%m-%d V=%V G=%G U=%U W=%W u=%u j=%j'
date -d '2027-01-01' '+%G-W%V-%u'2026-10-28 V=44 G=2026 U=43 W=43 u=3 j=301
2026-W53-5Three week numbers, three answers: 44 (ISO), 43 (%U, Sunday-based), 43 (%W, Monday-based). The second line is the trap — 1 January 2027 is a Friday, and ISO puts it in week 53 of 2026, so a weekly report keyed on %Y-%V files that day under the wrong year. Use %G with %V or %Y with %U/%W; mixing them is how December rows leak into January totals.
$ date -d 'last monday' +%F
date -d 'next friday 09:00' '+%F %H:%M %Z'
date -d '+90 minutes' '+%F %H:%M'2026-10-26
2026-10-30 09:00 CET
2026-10-28 10:33GNU's parser is a small natural-language engine, and it is the fastest way to build report windows in a script without arithmetic on day numbers. 'last monday' is the Monday strictly before today, 'next friday 09:00' keeps the time you give it, and relative offsets accept units (90 minutes, 2 days, 3 weeks). Everything it understands also works in touch -d and in find -newermt.
$ ts='27/Oct/2026:22:15:11 +0100'
date -d "$ts"
date -d "$(echo "$ts" | awk -F'[:/]' '{print $1"-"$2"-"$3" "$4":"$5":"$6}')" '+%F %T %Z -> %s'date: invalid date ‘27/Oct/2026:22:15:11 +0100’
2026-10-27 22:15:11 CET -> 1793135711The nginx/mysql timestamp with slashes is not parseable, which surprises people who just spent an hour on it. Reorder the date fields with one awk split and the same string parses, offsets applied. Once you have 1793135711 you can subtract two such lines to get a request duration, or compare them against the epoch of a deploy — which is the actual reason you were parsing in the first place.
Common flags
- -d, --date=STRING
- parse STRING instead of using the current time; accepts @epoch and free-form English
- +FORMAT
- output format; %F date, %T time, %s epoch, %z offset, %Z zone name, %V/%G ISO week and week-year
- -u, --utc
- display (and interpret) in UTC instead of local time
- -r, --reference=FILE
- show FILE's mtime instead of the current time
- -f, --file=DATEFILE
- parse one date per line, e.g. a column lifted out of a log
- -I[FMT], --iso-8601[=FMT]
- ISO 8601 output: -I date, -Iseconds date and time with offset
- -s, --set=STRING
- set the system clock (root only); changes the machine for every process
History
The -d flag nobody promised you
POSIX date formats the clock and, with the right privileges, sets it. It does not promise to parse 'next friday' — that whole parser comes from gnulib's parse_datetime, which GNU date, touch and a handful of other coreutils tools link in. So every runbook line containing date -d is a GNU line: on macOS and the BSDs the same flag means something else and the invocation fails, and the portable spelling is date -j -f '%Y-%m-%d' '2026-10-28' '+%s'. That single difference is why the 'works on my laptop' date snippet is a genre.
Unix time is not UTC
Unix time counts seconds since 1970 and deliberately ignores leap seconds, so there is no integer that represents 23:59:60 — date -d '2016-12-31 23:59:60' answers 'invalid date', not because of a parser gap but because the scale has no such value. Twenty-seven leap seconds have been inserted since 1972, which means UTC and the clock on your disk disagree by 27 seconds when you count from the epoch, and a system that needs real UTC (astronomy, some timing hardware) has to carry TAI offsets separately.
Fun facts
Pros & cons
pros
- + Parsing, formatting and arithmetic in one binary that is already on every Linux box
- + +%s gives you a plain integer, which sorts, subtracts and compares without any timezone reasoning
- + -r and TZ= answer file-age and 'what time is it there' questions without touching system config
cons
- − The -d parser is a GNU extension; the same command fails on macOS and BSD, where -j -f is the equivalent
- − No leap seconds and no sub-second precision beyond %N — fine for logs, wrong for anything that needs a monotonic clock
- − date -s is a sharp tool: it changes global state and there is no undo if you move the clock backwards
Takeaways
- 1Store and compare %s (epoch seconds); format to %F %T only at the edge, where a human reads it.
- 2Prefix a one-off check with TZ=Europe/Vienna instead of editing /etc/localtime — the change stays inside that process.
- 3Use date -r FILE +%s for file age; it is shorter than stat and needs no parsing.
- 4Pair %V with %G, and %U/%W with %Y — mixing an ISO week number with a calendar year puts new-year days in the wrong report.
- 5Expect DST: 02:30 does not exist on the spring-forward night, so schedule that job at 03:30 or in UTC.
Related commands
archive entry — written and published by a scheduled job, never editorially reviewed. Kept for reference: out of the site’s search index, and no ads run here. the reviewed library →
