Flash Sale:75% Off Hosting + Free DomainEnds in13h47m14sView Plans
Hostvento logoHostvento

Use the date Command in Linux

Show, format and calculate the date and time from the terminal.

VPS and Cloud Servers2 min read15 screenshots

Syntax

date [OPTION]... [+FORMAT]

OPTION changes how the command works. +FORMAT sets how the date and time are shown.

Show the date and time

date
Output of the date command

Set the system date

Use -s to set the date by hand. You need root rights.

sudo date -s "2024-11-05 14:30:00"
Setting the system date

On a server, it is better to let NTP or timedatectl keep the time right.

Show UTC time

date -u
The time in UTC

Calculate dates

Use -d to see a date relative to now:

date -d "2 days ago"
The date two days ago

Other examples: date -d "next Friday", date -d "tomorrow", date -d "1 month".

Custom format

date +"%Y-%m-%d %H:%M:%S"
A custom date format
CodeMeaning
%YYear with 4 digits
%mMonth number
%dDay of the month
%HHour (00-23)
%MMinute
%SSecond
%AFull weekday name
%BFull month name
%ZTime zone name
%sUnix time stamp

Weekday only

date +"%A"
The weekday name

Time zone

date +"%Z"
The time zone name

Unix timestamp (seconds since January 1, 1970)

date +"%s"
The Unix time stamp

Dates from a file

The -f option reads dates from a file, one per line, and prints them:

date -f dates.txt
Dates read from a file

Date of a file

The -r option shows the last change time of a file:

date -r date.txt
The last modified time of a file

Combine options

UTC with a custom format

date -u +"%Y-%m-%d %H:%M:%S UTC"
UTC time with a custom format

A file's modification date in a readable format

date -r /path/to/file +"%A, %d %B %Y %H:%M:%S"
A file's modification date

A relative date in a custom format

date -d "2 days ago" +"%Y-%m-%d"
A relative date with a format

A relative date in UTC

date -u -d "2 days ago" +"%Y-%m-%d %H:%M:%S UTC"
A relative date in UTC

Dates from a file in a custom format

date -f dates.txt +"%d/%m/%Y"
Dates from a file in a custom format

Quick recap

  • date shows the time, date -u shows UTC.
  • +FORMAT controls how it looks.
  • -d calculates dates and -r reads a file's time.