Linux Date Command

Linux Date Command Examples

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.



ls001a:~ # date
Mon Jun  3 09:33:22 BST 2013



Displaying the date from a passed string



ls001a:~ # date --date="12/25/2013"
Wed Dec 25 00:00:00 GMT 2013

ls001a:~ # date --date="Dec 25 2013"
Wed Dec 25 00:00:00 GMT 2013

Displaying Past Dates



john@ls001a:~/dates> date --date="yesterday"
Sun Jun  2 10:25:34 BST 2013

john@ls001a:~/dates> date --date="2 day ago"
Sat Jun  1 10:26:26 BST 2013

john@ls001a:~/dates> date --date="5 day ago"
Wed May 29 10:26:40 BST 2013

john@ls001a:~/dates> date --date="1 month ago"
Fri May  3 10:27:01 BST 2013

john@ls001a:~/dates> date --date="1 year ago"
Sun Jun  3 10:27:09 BST 2012

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:



john@ls001a:~/dates> cat dates.txt
25 Dec 2013
1 Dec 2013
July 4 2014
1 Aug 2015

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



john@ls001a:~/dates> date --file=dates.txt
Wed Dec 25 00:00:00 GMT 2013
Sun Dec  1 00:00:00 GMT 2013
Fri Jul  4 00:00:00 BST 2014
Sat Aug  1 00:00:00 BST 2015

Displaying the last modification time of a file


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



john@ls001a:~/dates> date --reference dates.txt
Mon Jun  3 10:15:49 BST 2013

Display Coordinated Universal Time UTC



john@ls001a:~/dates> date --utc
Mon Jun  3 09:22:35 UTC 2013

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 issued with no parameters. We next issue the date command with the "-s" option which allows us to pass our date and time settings:



[root@rhel01 root]# date
Mon Jun  3 10:50:24 BST 2013

[root@rhel01 root]# date -s "Tue June 5 19:00 BST 2014"
Thu Jun  5 19:00:00 BST 2014

[root@rhel01 root]# date -s "Mon Jun 3 10:53 BST 2013"
Mon Jun  3 10:53:00 BST 2013

[root@rhel01 root]# date
Mon Jun  3 10:53:03 BST 2013


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



ls001a:~ # date
Mon Jun  3 11:17:01 BST 2013

Display week number with date command



ls001a:~ # date +%W
22

Display month number with date command



ls001a:~ # date +%m
06

Display day of year number with date command



ls001a:~ # date +%j
154

Display day of week with date command



ls001a:~ # date +%A
Monday

Display Month with date command



ls001a:~ # 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 Monday This report was generated at  21:10:42