>_cmd.script

date

Display or set the system date and time

System

By CMD Script Team · 3 min read · Last updated

SYNTAX
date [OPTIONS] [+FORMAT]

Options

Command options and flags
FlagDescription
+FORMATPrint the date/time using a custom format string, e.g. date +"%Y-%m-%d %H:%M:%S"
-uPrint or set the time in UTC instead of local time
-dDisplay a specified date/time string instead of now, e.g. date -d "next friday"
-sSet the system date/time to the given string (requires root)
-IPrint date in strict ISO 8601 format, e.g. 2026-07-05
-RPrint date in RFC 5322 format, commonly used in email headers
--rfc-3339Print 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 format
  • date +%Y — print just the current year
  • date +"%Y-%m-%d" — print the date as 2026-07-05
  • date -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" +%F or date -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:%S instead of date +"%Y-%m-%d %H:%M:%S" — the shell will treat the unquoted part after a space as a separate argument and date will error.
  • Using GNU-only -d/-s relative-date syntax on macOS and getting a syntax error, since BSD date uses -v for adjustments instead.
  • Assuming date automatically accounts for timezone conversions without -u or TZ=, leading to confusion when comparing timestamps across servers in different zones.
  • Manually setting the clock with date -s on a system that also runs ntpd/chronyd — the time service will likely override the manual change shortly after.

Tips

  • Use date +%s to 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 -d relative syntax, or detect the platform and branch logic accordingly.

Best practices

  • Prefer NTP (chronyd, systemd-timesyncd, or ntpd) to keep server clocks synchronized instead of manually setting time with date -s, which drifts and can cause certificate/logging issues.
  • Standardize on ISO 8601 (date -I or date +%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 date if 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 +%s for the epoch; date -d @<epoch> to convert it back to a readable date.
  • What's the difference between GNU date's -d and BSD date's -v? GNU date -d accepts free-text relative expressions like "+1 day" or "next friday"; BSD/macOS date -v uses adjustment flags like -v+1d instead, 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.