date
Display or set the system date and time
By CMD Script Team · 3 min read · Last updated
date [OPTIONS] [+FORMAT]Options
| Flag | Description |
|---|---|
+FORMAT | Print the date/time using a custom format string, e.g. date +"%Y-%m-%d %H:%M:%S" |
-u | Print or set the time in UTC instead of local time |
-d | Display a specified date/time string instead of now, e.g. date -d "next friday" |
-s | Set the system date/time to the given string (requires root) |
-I | Print date in strict ISO 8601 format, e.g. 2026-07-05 |
-R | Print date in RFC 5322 format, commonly used in email headers |
--rfc-3339 | Print date/time in RFC 3339 format at the given precision (date, seconds, or ns) |
Distribution compatibility
- Ubuntu
- Debian
- Fedora
- Arch
- macOS (BSD date, different -d/-s syntax; use -j -f for parsing)
What it does
date prints (or, with root privileges, sets) the current system date and time. Its
real power comes from the +FORMAT syntax, which lets you print the date/time in any
custom layout using format specifiers like %Y (year), %m (month), %d (day), %H
(hour), %M (minute), and %S (second) — extremely common in scripts, logs, and
generated filenames.
Beginner examples
date— print the current date and time in the default locale formatdate +%Y— print just the current yeardate +"%Y-%m-%d"— print the date as2026-07-05date -u— print the current time in UTC
date +"%Y-%m-%d %H:%M:%S"
Advanced examples
- Generate a timestamp safe for filenames:
date +"%Y%m%d_%H%M%S" - Compute a relative date:
date -d "+7 days" +%Fordate -d "last monday" +%F - Print ISO 8601 date only:
date -I - Print RFC 5322 format (used in email
Date:headers):date -R - Convert a Unix epoch timestamp to a readable date:
date -d @1751500800 - Get the current epoch timestamp for scripting:
date +%s
date -d "+7 days" +%F
Common mistakes
- Forgetting quotes around format strings containing spaces, e.g. writing
date +%Y-%m-%d %H:%M:%Sinstead ofdate +"%Y-%m-%d %H:%M:%S"— the shell will treat the unquoted part after a space as a separate argument anddatewill error. - Using GNU-only
-d/-srelative-date syntax on macOS and getting a syntax error, since BSDdateuses-vfor adjustments instead. - Assuming
dateautomatically accounts for timezone conversions without-uorTZ=, leading to confusion when comparing timestamps across servers in different zones. - Manually setting the clock with
date -son a system that also runsntpd/chronyd— the time service will likely override the manual change shortly after.
Tips
- Use
date +%sto get Unix epoch seconds, handy for timestamping log entries or comparing durations in scripts. date -d "@<epoch>"converts an epoch timestamp back into a human-readable date.- For portable scripts that might run on both Linux and macOS, avoid GNU-specific
-drelative syntax, or detect the platform and branch logic accordingly.
Best practices
- Prefer NTP (
chronyd,systemd-timesyncd, orntpd) to keep server clocks synchronized instead of manually setting time withdate -s, which drifts and can cause certificate/logging issues. - Standardize on ISO 8601 (
date -Iordate +%F) for logs and filenames — it sorts correctly as plain text and avoids locale-dependent month/day ordering ambiguity. - When writing cross-platform scripts, test date-arithmetic commands on both GNU and BSD
dateif macOS support matters, since the flags for relative dates differ completely.
Try it yourself
A simulated shell with a sample home directory — experiment freely, nothing leaves your browser. Type help to list supported commands.
Real-world use cases
- Generating timestamped log file or backup archive names, e.g.
backup_$(date +%Y%m%d).tar.gz. - Comparing "now" against a deadline or expiration date in a deployment or cron script.
- Converting Unix epoch timestamps from application logs into human-readable dates for debugging.
- Adding a
Date:header in raw email or HTTP messages using RFC 5322 format (date -R).
Common interview questions
- How would you generate a timestamp safe to use in a filename?
date +"%Y%m%d_%H%M%S"— no spaces or slashes, sorts lexicographically. - How do you get the current Unix epoch time, and convert it back?
date +%sfor the epoch;date -d @<epoch>to convert it back to a readable date. - What's the difference between GNU date's
-dand BSD date's-v? GNUdate -daccepts free-text relative expressions like"+1 day"or"next friday"; BSD/macOSdate -vuses adjustment flags like-v+1dinstead, and the two are not interchangeable.
Frequently Asked Questions
How do I format the date for use in a filename?
Use a custom format string with no spaces, e.g. date +"%Y%m%d_%H%M%S" produces something like 20260705_143210, which is safe to embed in filenames.
How do I compute a date in the future or past?
Use the -d flag with a relative expression, e.g. date -d "+7 days" or date -d "3 weeks ago", both of which GNU date understands natively.
How is GNU date different from macOS/BSD date?
GNU date (Linux) accepts flexible -d "free text" expressions like 'next monday'; BSD date (macOS) instead uses -v for adjustments (date -v+1d) and -j -f for parsing custom input formats — the flag sets are not interchangeable.
Cheat sheet
Download a quick-reference cheat sheet for date.