LinuxCommandLibrary

date

display or set the system date and time

TLDR

Show current date and time

$ date
copy
Custom format
$ date ["+%Y-%m-%d %H:%M:%S"]
copy
Show date in UTC
$ date -u
copy
Set date (requires root)
$ sudo date -s ["2024-01-15 10:30:00"]
copy
Show date from timestamp
$ date -d [@1234567890]
copy

SYNOPSIS

date [options] [+format]

DESCRIPTION

date displays or sets the system date and time. It supports extensive output formatting through format specifiers, time zone conversions, relative date calculations, and Unix timestamp conversions. The command is essential for timestamping, logging, scheduling, and time-based operations.
Beyond simple display, date excels at date arithmetic: you can specify relative dates like "tomorrow", "next monday", "2 days ago", or "+3 hours". It can parse dates from strings, convert Unix timestamps to human-readable format, and format output for any purpose from ISO 8601 standards to custom formats.
The tool is one of the most commonly used Unix utilities, present since the earliest Unix versions. It's frequently used in shell scripts for generating timestamped filenames, calculating time differences, or performing date-based conditional logic.
Format specifiers allow precise control over output, from simple "YYYY-MM-DD" to complex locale-specific formats. The tool respects timezone settings and can display UTC or any specific timezone.

PARAMETERS

-d, --date=string

Display specified date/time
-s, --set=string
Set system date/time (requires root)
-u, --utc
Print/set UTC time
-r, --reference=file
Display file's modification time
-I[timespec]
ISO 8601 format
-R, --rfc-email
RFC 5322 format

FORMAT SPECIFIERS

Date:
- %Y - Year (2024)
- %y - Year (24)
- %m - Month (01-12)
- %d - Day (01-31)
- %B - Month name (January)
- %b - Short month (Jan)
- %A - Weekday (Monday)
- %a - Short weekday (Mon)
Time:
- %H - Hour 24h (00-23)
- %I - Hour 12h (01-12)
- %M - Minute (00-59)
- %S - Second (00-59)
- %p - AM/PM
- %Z - Timezone name
Combined:
- %s - Unix epoch seconds
- %F - Full date (YYYY-MM-DD)
- %T - Full time (HH:MM:SS)
- %c - Locale's date and time

WORKFLOW

$ # Current date/time
date

# ISO 8601 format
date -I
date "+%Y-%m-%d"

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

# Unix timestamp
date +%s

# From timestamp
date -d @1234567890

# Relative dates
date -d "tomorrow"
date -d "next monday"
date -d "2 days ago"
date -d "+3 hours"

# UTC time
date -u

# File modification time
date -r file.txt

# RFC format
date -R
copy

RELATIVE DATES

$ date -d "1 day ago"
date -d "2 weeks ago"
date -d "next friday"
date -d "last month"
date -d "3 years ago"
date -d "+5 days"
copy

COMMON USES

Timestamped filenames:

$ backup-$(date +%Y%m%d).tar.gz
log-$(date +%Y-%m-%d_%H-%M-%S).txt
copy
Logging:
$ echo "$(date): Starting backup" >> log.txt
copy
Conditionals:
$ if [ $(date +%u) -eq 1 ]; then
    echo "It's Monday"
fi
copy

CAVEATS

Setting date requires root. System clock vs hardware clock different. Timezone affects output. Leap seconds not always handled. Different behavior on BSD vs GNU. Relative dates parsing can be ambiguous.

HISTORY

date has been part of Unix since Version 1 in 1971, one of the original Unix commands.

SEE ALSO

timedatectl(1), hwclock(8), cal(1)

> TERMINAL_GEAR

Curated for the Linux community

Copied to clipboard

> TERMINAL_GEAR

Curated for the Linux community