Date Command in Linux

saurabh kharkate
3 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.

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
  • 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"
  • 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.

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

Thanks for reading!

--

--