Skip to content

date Command Cheat Sheet

The date command displays or sets the system date and time. It is essential for scripting, logging, and time arithmetic in Linux.


Synopsis

date [OPTION]... [+FORMAT]
date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]

Displaying Date and Time

Standard Output

date
# Output: Sat Feb 14 20:30:00 UTC 2026

UTC Time (-u)

Display Coordinated Universal Time.

date -u

ISO 8601 Format (-I)

Output date in ISO 8601 format (YYYY-MM-DD).

date -I
# Output: 2026-02-14

With precision:

date -Iseconds
# Output: 2026-02-14T20:30:00+00:00

RFC 2822 Format (-R)

Standard email date format.

date -R
# Output: Sat, 14 Feb 2026 20:30:00 +0000

Formatting Output

You can format the output using + followed by format specifiers.

date "+%Y-%m-%d %H:%M:%S"
# Output: 2026-02-14 20:30:00

Common Format Specifiers

Specifier Description Example
%Y Year (4 digits) 2026
%y Year (2 digits) 26
%m Month (01-12) 02
%B Full month name February
%b Abbreviated month Feb
%d Day of month (01-31) 14
%A Full weekday name Saturday
%a Abbreviated weekday Sat
%H Hour (00-23) 20
%I Hour (01-12) 08
%M Minute (00-59) 30
%S Second (00-59) 00
%p AM/PM PM
%Z Timezone abbreviation UTC
%z Timezone offset +0000
%F Full date (same as %Y-%m-%d) 2026-02-14
%T Time (same as %H:%M:%S) 20:30:00
%s Seconds since 1970-01-01 (Epoch) 1771101000

Date Arithmetic and Conversion (-d)

The --date or -d flag is powerful for calculating past/future dates or converting formats.

Relative Dates

date -d "tomorrow"
date -d "yesterday"
date -d "next Friday"
date -d "last Monday"
date -d "2 days ago"
date -d "3 months 1 day"
date -d "-1 hour"

Convert Epoch to Date

Convert a Unix timestamp (seconds since 1970) to human-readable format, prepend @.

date -d @1771101000

Convert Date to Epoch

date -d "2026-02-14 20:30:00" +%s

Parsing Custom Strings

date is smart enough to parse many formats.

date -d "14 Feb 2026"
date -d "2026/02/14"

Setting Date and Time (-s)

Usually requires sudo.

# Set specific date and time
sudo date -s "2026-02-14 20:30:00"

# Set date only (time set to 00:00)
sudo date -s "2026-02-15"

# Set time only
sudo date -s "21:00:00"

Timezone Handling

You can set the TZ environment variable for a single command to see the time in another zone.

# View time in New York
TZ="America/New_York" date

# View time in Tokyo
TZ="Asia/Tokyo" date

List available timezones:

timedatectl list-timezones


Practical Examples for Scripts

Filename with Timestamp

Create a backup file with the current date/time in the name.

filename="backup_$(date +%Y%m%d_%H%M%S).tar.gz"
tar -czf "$filename" /var/www/html

Measuring Execution Time

Arguments %s (seconds) and %N (nanoseconds) help measure duration.

start=$(date +%s)
sleep 2
end=$(date +%s)

duration=$((end - start))
echo "Task took $duration seconds."

Checking for End of Month

How to check if tomorrow is a new month?

# Add 1 day to current date, check day format
if [ "$(date -d "+1 day" +%d)" = "01" ]; then
    echo "Tomorrow is the first of the month."
fi

Get Start/End of Day

# Start of today (00:00:00)
date -d "today 00:00"

# End of today (23:59:59 matches logic better than 24:00)
date -d "tomorrow 00:00 -1 second"

Other Utilities

  • timedatectl: Modern replacement for setting time, timezone, and NTP sync on systemd systems.
  • hwclock: Access the hardware clock (BIOS/RTC).

Tips

  • Reproducibility: When writing logs, always include timezone (%z) or use UTC (-u) to avoid confusion.
  • Precision: %N gives nanoseconds, but standard date implementations vary. GNU date supports it fully.