date formats and timestamps
Build date’s output format, try the time strings -d understands, change the time zone, convert between timestamps and dates. The results are those of GNU date on real Linux.
2026-11-24 22:09:23
date '+%Y-%m-%d %H:%M:%S'In a crontab, escape the %: date '+\%Y-\%m-\%d \%H:\%M:\%S'
| %Y | 2026 | Year, four digits |
| %m | 11 | Month (01–12) |
| %d | 24 | Day of the month (01–31) |
| %H | 22 | Hour, 24-hour clock (00–23) |
| %M | 09 | Minute (00–59) |
| %S | 23 | Second (00–60) |
The format has spaces or special characters, so +FORMAT is quoted in the command; unquoted, the shell would split it (date: extra operand).
Things that need nested $( … ) or arithmetic. After the arrow: what they print in the simulator, in the zone above, right now.
The first of this month
date +%Y-%m-01→ 2026-11-01
The last day of this month
date -d "$(date +%Y-%m-01) +1 month -1 day" +%F→ 2026-11-30
Back to the 1st, one month on, one day back
Last month (the safe way)
date -d "$(date +%Y-%m-15) -1 month" +%Y-%m→ 2026-10
Use the 15th: at the end of a month, 1 month ago from March 31 gives March 3
Monday of this week
date -d "$(( $(date +%u) - 1 )) days ago" +%F→ 2026-11-23
%u is the weekday (1–7); minus 1 is how far back Monday is. last monday on a Monday gives the week before
The timestamp of today’s midnight
date -d 'today 00:00' +%s→ 1795496400
The date 7 days ago
date -d '7 days ago' +%F→ 2026-11-17
For log file names, find -newermt
Days until December 25
echo $(( ($(date -d 2026-12-25 +%s) - $(date +%s)) / 86400 ))→ 30
Two timestamps differ in seconds; divide by 86400 for days
Is it the weekend?
[ $(date +%u) -ge 6 ] && echo weekend || echo weekday→ weekday
The date in a file name
echo "backup-$(date +%F).tar.gz"→ backup-2026-11-24.tar.gz
Timestamp in milliseconds
date +%s%3N→ 1795576163234
How long a script took
start=$(date +%s); sleep 2; echo "took $(( $(date +%s) - start ))s"Note the timestamp at the start and subtract at the end. Time stands still in the simulator, so this one is not previewed
Click to add one to the end of the format; the value is for the time above.
Date
Months and weekdays
Weeks
Time
Zone
Other
The timestamp now: 1795576163 · date +%s
Tue Nov 14 17:13:20 EST 2023
Tue Nov 14 22:13:20 UTC 2023
date -d @17000000001767243600
date -d '2026-01-01 00:00:00' +%sThe same "2026-01-01 00:00" is a different moment, and timestamp, in each zone; say which (UTC, +08:00) to be sure.
- The format starts with +: date +%F. With spaces in it, quote it: date '+%F %T', or you get "extra operand".
- A capital %M is the minute, a lowercase %m the month; %S is the second, %s the timestamp.
- %-d and %-m drop the leading zero (5, not 05); %3N gives milliseconds.
- -d understands yesterday, tomorrow, 3 days ago, +1 week, next monday, @1700000000 and the like.
- Do not follow a time with +3: in 12:00 +3 days the +3 is taken as a time zone.
- -u uses UTC; TZ=Asia/Shanghai date switches zones for one command (the site’s real Linux image has no zone database: use TZ=CST-8 there).
- -I prints ISO 8601 (-Iseconds with the time), -R the mail format; -r FILE shows when the file was last changed.
- In a crontab % is special: write \%, as in date +\%F.
- To debug a -d string, add --debug on real Linux (date --debug -d '…'): GNU date prints step by step how it read it. The simulator does not have it; "How the time is worked out" on this page does the same job.