date Command - Formatting and Calculating Dates
What Is the date Command?
date command is for. Run it and it prints the current time, and date +%Y%m%d formats it as 20260605. You can drop that straight into a file name.The date command displays the current date and time, and formats it however you like. Log timestamps, backup file names, date math inside scripts — it's a command you'll reach for almost every day.
In one line
date +FORMAT builds a date string in any shape you want.
Example: date +%Y-%m-%d → 2026-06-05
What You'll Learn
- Show the current time with
date, and format it withdate +%Fand friends - Combine format specifiers like
%Y%m%dinto any layout - Calculate dates such as yesterday, N days later, next week with
date -d - Get epoch seconds (Unix time) with
date +%sand convert back withdate -d @seconds - Switch time zones with
TZ='UTC' dateordate -u - Watch out for the syntax differences between Linux (GNU date) and macOS (BSD date)
1. Show the Current Time
Conclusion: Run date with no arguments for the current time; use date +FORMAT to change how it looks.
With no arguments, date prints the current date and time.
$ date Fri Jun 5 15:52:59 JST 2026
The exact text (the weekday and the JST part) depends on your system's locale and time zone settings.
When you want just the date, or just the time, use the format specifiers in the next section.
2. Format the Output (%Y %m %d…)
Conclusion: Put specifiers like %Y (year), %m (month), %d (day) after date + to shape the output.
Write format specifiers after the + and you get exactly that shape.
$ date +%Y-%m-%d 2026-06-05
$ date "+%Y/%m/%d %H:%M:%S" 2026/06/05 15:52:59
If your format contains a space, wrap the whole thing in "..." (double quotes). Without quotes, the shell splits it on the space and you get an error or unexpected output.
2-1. Common Format Specifiers
| Specifier | Meaning | Example |
|---|---|---|
%Y |
4-digit year | 2026 |
%m |
Month (01–12) | 06 |
%d |
Day (01–31) | 05 |
%H |
Hour (00–23) | 15 |
%M |
Minute (00–59) | 52 |
%S |
Second (00–59) | 59 |
%A |
Weekday (full) | Friday |
%a |
Weekday (short) | Fri |
%j |
Day of the year | 156 |
%s |
Epoch seconds | 1780642379 |
2-2. Handy Shortcuts
$ date +%F 2026-06-05
$ date +%T 15:52:59
%Fequals%Y-%m-%d(the ISO 8601 date)%Tequals%H:%M:%S(the time)
When in doubt, remember %F (date) and %T (time) — they cover most situations.
3. Templates for File Names and Logs
Conclusion: Embed $(date +%Y%m%d) in a file name to create dated files automatically.
$(...) inserts a command's output into a string (command substitution). It's the classic trick for backups.
$ cp data.db "backup-$(date +%Y%m%d).db"
This creates a file named backup-20260605.db.
$ echo "[$(date '+%Y-%m-%d %H:%M:%S')] Process started" >> app.log
Use this when you want to add your own timestamp to a log. Example output:
[2026-06-05 15:52:59] Process started
Avoid putting %T directly in file names, since it contains a : (colon). Some environments and file systems handle that poorly. For a time stamp in a name, %H%M%S (no separators) is safer.
4. Calculate Dates (yesterday, 3 days later, N days ago)
Conclusion: Use date -d "string" to calculate dates like yesterday, 3 days ago, or next friday in plain English.
Pass the date you want to the -d (or --date) option, written in plain English.
$ date -d "yesterday" +%F 2026-06-04
$ date -d "3 days ago" +%F 2026-06-02
$ date -d "next friday" +%F 2026-06-12
You can also calculate from a given base date.
$ date -d "2025-01-01 +30 days" +%F 2025-01-31
date does that for you correctly. It handles month-ends and leap years too, so it's far safer than counting in your head.Common expressions
tomorrow/yesterday3 days ago/2 weeks/1 monthnext monday/last sunday
This relative date math is a GNU date (Linux) feature. macOS uses a different syntax and these won't work there. See Section 7 for details.
5. Epoch Seconds (Unix Time)
Conclusion: date +%s prints seconds since 1970, and date -d @seconds converts them back to a date.
Epoch seconds (Unix time) is the number of seconds since 1970-01-01 00:00:00 UTC. It's a common format for storing time in programs and logs.
$ date +%s 1780642379
To convert epoch seconds back into a human-readable date, prefix the number with @.
$ date -u -d @1700000000 "+%Y-%m-%d %H:%M:%S %Z" 2023-11-14 22:13:20 UTC
When a log records only a number like 1700000000 and you can't read it, date -d @that-number converts it to a date instantly.
6. Switch Time Zones (TZ / -u)
Conclusion: date -u shows UTC, and TZ='Area/City' date shows the time in any time zone.
Add -u to display the time in UTC (Coordinated Universal Time).
$ date -u "+%Y-%m-%d %H:%M %Z" 2026-06-05 06:52 UTC
To see the time in a specific region, set the TZ environment variable right before the command.
$ TZ='America/New_York' date "+%Y-%m-%d %H:%M %Z" 2026-06-05 02:52 EDT
$ TZ='Asia/Tokyo' date "+%Y-%m-%d %H:%M %Z" 2026-06-05 15:52 JST
TZ=... before the command changes the time zone just for that one run?TZ='Area/City' date only applies while that date runs. Your system-wide setting stays untouched, so it's safe to experiment.You can list the valid zone names (like Asia/Tokyo, America/New_York, Europe/London) with timedatectl list-timezones. Since TZ is an environment variable, reading Working with Environment Variables will deepen your understanding.
7. Mind the Difference with macOS (BSD date)
Conclusion: Linux ships GNU date; macOS ships BSD date, whose date math and epoch syntax differ.
The commands in this article assume Linux such as Ubuntu (GNU coreutils date). The date that ships with macOS is the BSD version, and some options differ.
| What you want | Linux (GNU) | macOS (BSD) |
|---|---|---|
| 3 days ago | date -d "3 days ago" |
date -v-3d |
| Epoch → date | date -d @1700000000 |
date -r 1700000000 |
| Parse a date string | date -d "2025-01-01" |
date -j -f "%Y-%m-%d" "2025-01-01" |
If a date -d ... command you found online complains with something like illegal option on macOS, you're likely on the BSD version. Run date --version; if it prints GNU coreutils, you're on the GNU version.
Mini challenge: show the date 3 days from now in YYYY/MM/DD format
Hint: calculate the date with -d, then shape it with +FORMAT.
Answer:
$ date -d "3 days" "+%Y/%m/%d" 2026/06/08
"3 days" means "3 days from now." Writing "+3 days" does the same thing.
Summary
| Task | Command |
|---|---|
| Show the current time | date |
| Show the date only | date +%F (= date +%Y-%m-%d) |
| Show the time only | date +%T |
| Date for a file name | date +%Y%m%d |
| Calculate a date | date -d "3 days ago" +%F |
| Get epoch seconds | date +%s |
| Convert epoch to a date | date -d @1700000000 |
| Show UTC | date -u |
| Use a specific time zone | TZ='Asia/Tokyo' date |
Three things to try right now
- Run
date +%Fto show today's date - Run
date -d "tomorrow" +%Fto calculate tomorrow - Run
cp something.txt "backup-$(date +%Y%m%d).txt"to make a dated file