date Command - Formatting and Calculating Dates
What you'll be able to do
- Shape dates and times any way you want with `date +FORMAT`
- Calculate dates such as yesterday or N days later with `date -d`
- Convert between epoch seconds and dates with `date +%s` and `date -d @seconds`
Prerequisites (read these first)
What Is the date Command?
date command is for. Type it and it prints the current time.date +%Y%m%d and it formats that as 20260605. You can drop the result straight into a file name.The date command shows the current date and time. It can also shape that output however you like.
You will use it for log timestamps, backup file names, and date math inside scripts. It comes up almost every day.
Words used here (sorted out first)
- Format: how the date should be printed. "Layout" and "shape" mean the same thing here.
- Format specifier: a symbol starting with
%, such as%Y. Each one stands for a single piece of the date. - Locale: your language and region settings. Weekday names and similar text change with it.
- Time zone: the time offset of a region. Japan uses JST, and the world reference is UTC.
- Epoch seconds: the number of seconds since 1 January 1970. "Unix time" means the same thing.
Environment
- OS: Ubuntu / typical Linux
- GNU coreutils
date(the BSD version on macOS uses a different syntax — see section 7) - The output in this article is shown in Japan time (JST). Times shift on machines set to another time zone
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 shape it withdate +%Fand friends - Combine format specifiers like
%Y,%m, and%dinto any layout - Calculate dates such as yesterday, N days later, or next week with
date -d - Get epoch seconds with
date +%s, and convert back withdate -d @seconds - Switch time zones with
TZ='UTC' dateordate -u - Watch out for the 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 options at all, date prints the current date and time.
$ date
Fri Jun 5 15:52:59 JST 2026
The exact text depends on your system's locale and time zone settings. The weekday and the JST part are the parts that change.
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 +. You then 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 shellAn interactive program that reads the commands you type and runs them. splits the format at the space. date then treats the second half as an extra argument and stops with an error.
$ date +%Y-%m-%d %H:%M:%S
date: extra operand '%H:%M:%S' Try 'date --help' for more information.
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–60) | 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 for the date and %T for the time. Those two cover most situations.
2-3. Lina Gets Stuck: Mixing Up %m and %M
52. There is no such month.$ date +%Y-%M-%d
2026-52-05
%M.%m is the month, and uppercase %M is the minute. It is 15:52 right now, so 52 landed in the month slot.%Y for the year is the one exception.%m (month) and %M (minute) are easy to swap, and so are %d (day) and %D (month/day/year). A mistake does not raise an error. It prints a plausible-looking number instead, which is why checking the output matters.
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. This is called command substitution, and it is 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. %T contains a colon (:), and some environments and file systems handle that poorly.
To put a time in a name, use %H%M%S with no separators instead.
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 base date that you choose yourself.
$ date -d "2025-01-01 +30 days" +%F
2025-01-31
date does that part correctly. It knows how long each month is, and it handles leap years.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, so these will not 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.
Programs and logs often store time in this form. It does not depend on region or formatting, which makes it easy to compare.
$ date +%s
1780642379
To convert epoch seconds back into a readable date, prefix the number with @.
$ date -u -d @1700000000 "+%Y-%m-%d %H:%M:%S %Z"
2023-11-14 22:13:20 UTC
-u is what pins this output to UTC. Without it, the result follows your machine's time zone.
In Japan time (JST, UTC+9) the same command prints 2023-11-15 07:13:20, which is the next day. Machines set to a different zone show a different date, so add -u or TZ when you need a fixed answer.
A log sometimes records only a number like 1700000000, which you cannot read as it stands. date -d @that-number converts it to a date right away.
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 variableA named value that the shell or a program can look up while running. 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 one date runs.You can list the valid zone names with timedatectl list-timezones. Asia/Tokyo, America/New_York, and Europe/London are examples.
TZ is an environment variable, so 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, so some of its 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" |
A date -d ... command you found online may complain with something like illegal option on macOS. In that case you are most likely on the BSD version.
Run date --version. If it prints GNU coreutils, you are on the GNU version.
8. Mini Exercises: Try It Yourself
Conclusion: Three drills — building a format, calculating a date, and converting epoch seconds — confirm the basics of
dateby hand.
Task 1: Print today's date in the form 2026/06/05, using slashes.
Show hint 1 (direction)
After the +, line up the symbols for year, month, and day, joined by slashes. There are no spaces involved.
Show hint 2 (command name)
The command is date. The symbols for year, month, and day are in the table in section 2. Note that the month is lowercase.
Show answer
$ date +%Y/%m/%d
2026/06/05
Task 2: Print the date 3 days from now in YYYY/MM/DD form.
Show hint 1 (direction)
Two things at once. You need the option that calculates a date, plus the format that shapes it.
Show hint 2 (command name)
The command is date. The option that calculates a date is in section 4, and the format syntax is in section 2.
Show answer
$ date -d "3 days" "+%Y/%m/%d"
2026/06/08
"3 days" means "3 days from now". Writing "+3 days" gives the same result.
Task 3: Convert the epoch seconds 1700000000 back into a date in YYYY-MM-DD form, in UTC.
Show hint 1 (direction)
A bare number is not read as a date. Put a marker in front of it to say that it is epoch seconds. Add the option that switches the output to UTC.
Show hint 2 (command name)
The command is date. The marker for epoch seconds is in section 5, and the option that switches to UTC is in section 6.
Show answer
$ date -u -d @1700000000 +%F
2023-11-14
%F means the same as %Y-%m-%d. Without -u the answer follows your local time zone, so it can land on the next day.
9. Review
date +FORMAT decides the shape, and date -d calculates the date.%m is the month and %M is the minute. I'll always check that one on screen.@ when converting epoch seconds back.Today's 3-Line Summary
date +%Fgives the date anddate +%Tgives the time. When in doubt, use those two%mis the month and%Mis the minute. A mix-up raises no error, so check the outputdate -d "3 days ago"calculates dates, anddate -d @secondsturns epoch seconds back into a date
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