System

date

Print or set the system date and time.

timedateformatsystemtimestamp

Additional Notes

date prints the current date and time. It can also format timestamps, parse date expressions, show UTC, and set the system time when run with enough privileges.

It is commonly used in scripts to create timestamped filenames and logs.

Syntax

date [options] [+FORMAT]

Parameters

  • options: Flags that change how date behaves.
  • '+FORMAT': Custom output format string prefixed with +.

Common Options

  • -u, --utc: Use UTC.
  • -d TEXT, --date=TEXT: Display a parsed date expression.
  • -r FILE: Show the last modification time of a file.
  • -s TEXT, --set=TEXT: Set the system date/time.
  • +FORMAT: Print using a custom format.

Common Format Codes

  • %Y: Four-digit year.
  • %m: Month number.
  • %d: Day of month.
  • %H: Hour, 00-23.
  • %M: Minute.
  • %S: Second.
  • %F: Date as YYYY-MM-DD.
  • %T: Time as HH:MM:SS.
  • %s: Unix timestamp seconds.

Examples

date

Show current local date and time.

date -u

Show current UTC time.

date +"%Y-%m-%d %H:%M:%S"

Print a custom timestamp.

date +%s

Print Unix timestamp seconds.

date -d "tomorrow"

Parse a human date expression.

date -r file.txt

Show a file's modification time.

backup="backup-$(date +%F-%H%M%S).tar.gz"

Create a timestamped filename in a shell script.

Practical Notes

  • Setting system time usually requires sudo.
  • Many systems sync time automatically with NTP or systemd-timesyncd.
  • Use UTC for logs and servers when comparing events across time zones.
  • Quote format strings that contain spaces.