Linux Date Command

Date Command Examples

The date command is used to display or set your systems date and time.

Syntax: date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]

By issuing the date command without any additional parameters, the system's date and time is displayed.


# date
Wed  2 Jun 19:47:12 BST 2021

Displaying the date from a passed string


# date --date="6/2/2021"
Wed  2 Jun 00:00:00 BST 2021

# date --date="Jun 2 2021"
Wed  2 Jun 00:00:00 BST 2021

Displaying Past Dates


# date --date="yesterday"
Tue  1 Jun 19:54:10 BST 2021

# date --date="2 day ago"
Mon 31 May 19:54:24 BST 2021

# date --date="5 day ago"
Fri 28 May 19:54:33 BST 2021

# date --date="1 month ago"
Sun  2 May 19:54:41 BST 2021

# date --date="1 year ago"
Tue  2 Jun 19:54:50 BST 2020

Displaying dates from a file

To demonstrate the date command used with a file, I have created a file called "dates.txt". The content of the file is as below:


# cat dates.txt 
25 Dec 2018
1 Jan 2019
25 Dec 2019
1 Jan 2020
25 Dec 2020
1 Jan 2021

Now using the "--file" option, we can read the entries within the file and process these with the date command:


# date --file=dates.txt 
Tue 25 Dec 00:00:00 GMT 2018
Tue  1 Jan 00:00:00 GMT 2019
Wed 25 Dec 00:00:00 GMT 2019
Wed  1 Jan 00:00:00 GMT 2020
Fri 25 Dec 00:00:00 GMT 2020
Fri  1 Jan 00:00:00 GMT 2021

Displaying the last modification time of a file

The date command can also be used to display a files modification time:


# date --reference dates.txt 
Wed  2 Jun 20:14:41 BST 2021

Display Coordinated Universal Time UTC


# date --utc
Wed  2 Jun 19:19:39 UTC 2021

Setting Date and Time

The date command can be used to configure the date and time on your system. In the example below, we confirm the current date and time with the date command using no additional parameters. We next issue the date command with the "-s" option which allows us to pass our date and time settings:


# date
Wed  2 Jun 20:21:31 BST 2021

# date -s "Tue June 15 19:00 BST 2021"
Tue 15 Jun 19:00:00 BST 2021

# date -s "Wed June 23 19:00 BST 2021"
Wed 23 Jun 19:00:00 BST 2021

# date
Wed  2 Jun 20:23:48 BST 2021

Additional Date Formats

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

FORMAT controls the output. Interpreted sequences are:

The following date formats can be used in conjunction with the date command:


  %%   a literal %
  %a   locale's abbreviated weekday name (e.g., Sun)
  %A   locale's full weekday name (e.g., Sunday)
  %b   locale's abbreviated month name (e.g., Jan)
  %B   locale's full month name (e.g., January)
  %c   locale's date and time (e.g., Thu Mar  3 23:05:25 2005)
  %C   century; like %Y, except omit last two digits (e.g., 20)
  %d   day of month (e.g., 01)
  %D   date; same as %m/%d/%y
  %e   day of month, space padded; same as %_d
  %F   full date; same as %Y-%m-%d
  %g   last two digits of year of ISO week number (see %G)
  %G   year of ISO week number (see %V); normally useful only with %V
  %h   same as %b
  %H   hour (00..23)
  %I   hour (01..12)
  %j   day of year (001..366)
  %k   hour ( 0..23)
  %l   hour ( 1..12)
  %m   month (01..12)
  %M   minute (00..59)
  %n   a newline
  %N   nanoseconds (000000000..999999999)
  %p   locale's equivalent of either AM or PM; blank if not known
  %P   like %p, but lower case
  %r   locale's 12-hour clock time (e.g., 11:11:04 PM)
  %R   24-hour hour and minute; same as %H:%M
  %s   seconds since 1970-01-01 00:00:00 UTC
  %S   second (00..60)
  %t   a tab
  %T   time; same as %H:%M:%S
  %u   day of week (1..7); 1 is Monday
  %U   week number of year, with Sunday as first day of week (00..53)
  %V   ISO week number, with Monday as first day of week (01..53)
  %w   day of week (0..6); 0 is Sunday
  %W   week number of year, with Monday as first day of week (00..53)
  %x   locale's date representation (e.g., 12/31/99)
  %X   locale's time representation (e.g., 23:13:48)
  %y   last two digits of year (00..99)
  %Y   year
  %z   +hhmm numeric time zone (e.g., -0400)
  %:z  +hh:mm numeric time zone (e.g., -04:00)
  %::z  +hh:mm:ss numeric time zone (e.g., -04:00:00)
  %:::z  numeric time zone with : to necessary precision (e.g., -04, +05:30)
  %Z   alphabetic time zone abbreviation (e.g., EDT)

Date command without any formatting


# date
Wed  2 Jun 20:32:57 BST 2021

Display week number with date command


date +%W
22

Display month number with date command


# date +%m
06

Display day of year number with date command


# date +%j
153

Display day of week with date command


# date +%A
Wednesday

Display Month with date command


date +%B
June

Using date commands within a script or report

It is often useful to add some date information to a report. This can easily be achieved by using the date command with the above FORMAT options:


# echo "Today is "`date +%A` "This report was generated at " `date +%T`
Today is Wednesday This report was generated at  20:36:37