Date Command in Linux

Kaushik Denge
4 min readSep 24, 2021

date command is used to display the system date and time. date command is also used to set date and time of the system. By default the date command displays the date in the time zone on which unix/linux operating system is configured.You must be the super-user (root) to change the date and time.

Syntax:

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

Options with Examples

  • date (no option) : With no options, the date command displays the current date and time, including the abbreviated day name, abbreviated month name, day of the month, the time separated by colons, the time zone name, and the year.
  • List of Format specifiers used with date command:
%D: Display date as mm/dd/yy.       
%d: Display the day of the month (01 to 31).
%a: Displays the abbreviated name for weekdays (Sun to Sat).
%A: Displays full weekdays (Sunday to Saturday).
%h: Displays abbreviated month name (Jan to Dec).
%b: Displays abbreviated month name (Jan to Dec).
%B: Displays full month name(January to December).
%m: Displays the month of year (01 to 12).
%y: Displays last two digits of the year(00 to 99).
%Y: Display four-digit year.
%T: Display the time in 24 hour format as HH:MM:SS.
%H: Display the hour.
%M: Display the minute.
%S: Display the seconds.

Displaying Universal Time

If your system time zone is based on your local time zone and you want to check the universal time, to do so we need to add the -u option to the command which refers to UTC.

# date -u

In the following example we will format date in yyyy-MM-dd format

# date  +%Y-%m-%d

Displaying Date From String

We can display the formatted date from the date string provided by the user using the -d or –date option to the command. It will not affect the system date, it only parses the requested date from the string. For example,

#date -d "May 25 2001"

Displaying Upcoming Date & Time With -d Option

Aside from parsing the date, we can also display the upcoming date using the -d option with the command. The date command is compatible with words that refer to time or date values such as next Sun, last Friday, tomorrow, yesterday, etc. For examples,

Displaying Next Monday Date

# date -d “next Tue”

Displaying Past Date & Time With -d Option
Using the -d option to the command we can also know or view past date. For examples,

Displaying Last Friday Date
# date -d “last Fri”

Setting Date & Time on Linux
We can not only view the date but also set the system date according to your preference. For this, you can execute the command in the following way.

# date -s “Sun 30 May 2021 07:35:06 pm”

Use Epoch Time (Epoch Converter)

You can use the date command as an Epoch converter. Epoch, timestamps, is the number of seconds that have passed since January 1, 1970, at 00:00:00 UTC.

To show the number of seconds from the epoch to the current day, use the %s format control:

date +%s

To see how many seconds passed from epoch to a specific date, enter:

#date -d "2001-05-25" +%s

Thanks for reading!

--

--