date command in Linux with Examples


Advertisements

Name

date - print or set the system date and time

Synopsis

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

Options

-d, --date=STRING
   display time described by STRING, not 'now'

-f, --file=DATEFILE
   like --date; once for each line of DATEFILE

-I[FMT], --iso-8601[=FMT]
   output date/time in ISO 8601 format. FMT='date' for date only (the default), 'hours', 'minutes', 'seconds', or 'ns' for date and time to the indicated precision. Example: 2006-08-14T02:34:56-0600

-R, --rfc-2822
   output date and time in RFC 2822 format. Example: Mon, 14 Aug 2006 02:34:56 -0600

--rfc-3339=FMT
   output  date/time in RFC 3339 format. FMT='date', 'seconds', or 'ns' for date and time to the indicated precision. Example: 2006-08-14 02:34:56-06:00

-r, --reference=FILE
   display the last modification time of FILE

-s, --set=STRING
   set time described by STRING

-u, --utc, --universal     
   print or set Coordinated Universal Time (UTC)

-V, --version
   Print the version number of accton to standard output and quit.

-h, --help
   Prints the usage information to standard output and quit.

Description

date command displays the current time and date (of the system) in the given FORMAT. date command is also used by system administrator to set the system date.

Invoking ‘date’command with no FORMAT argument displays system date in dafault FORMAT. The default FORMAT depends on the ‘LC_TIME’ environment variable. In the default C locale, this format is ‘'+%a %b %e %H:%M:%S %Z %Y'’, so the output looks like ‘Mon Aug 3 00:00:00 IST 2020’. Default format includes the abbreviated day name, abbreviated month name, day of the month, the time separated by colons, the time zone name, and the year.

Arguments that are mandatory to long options are mandatory for short options also.

Options

Tag Description
-d, --date=STRING display time described by STRING, not the current time of the system or 'now'
-f, --file=DATEFILE like --date; once for each line of DATEFILE, a line in DATEFILE may contain text like '2 days ago' or 'now' or 'tomorrow' or 'next Monday', etc.
-I[FMT], --iso-8601[=FMT] output date/time in ISO 8601 format. FMT='date' for date only (the default), 'hours', 'minutes', 'seconds', or 'ns' for date and time to the indicated precision.
-R, --rfc-2822 output date and time in RFC 2822 format.
--rfc-3339=FMT output date/time in RFC 3339 format. FMT='date', 'seconds', or 'ns' for date and time to the indicated precision.
-s, --set=STRING set time described by STRING
-u, --utc, --universal print or set Coordinated Universal Time (UTC)
--help display help on command line and exit
--version output version information and exit

Arguments

FORMAT argument can consist of several format specifiers, these specifiers are of various kinds, like Time Conversion specifiers, Date Conversion specifiers or literals.

If given an argument that starts with a ‘+’, ‘date’ prints the current date and time in the format defined by that argument. Arguments can consist of format conversion specifiers. Format conversion specifiers are described below.

Time Conversion Specifiers: %[HIklMNpPrRsSTXzZ]

Literal conversion specifiers: %[%nt]

Time Specifier Description
%H hour ('00'...'23'), 24 hour time format.
%I hour ('01'...'12') 12 hour time format
%k hour is space padded (‘ 0’...‘23’); equivalent to ‘%_H’. This is a GNU extension.
%l hour, space padded (‘ 1’...‘12’); equivalent to ‘%_I’. This is a GNU extension
%M minute (‘00’...‘59’)
%N nanoseconds (‘000000000’...‘999999999’). This is a GNU extension.
%p locale’s equivalent of either ‘AM’ or ‘PM’; blank in many locales. Noon is treated as ‘PM’ and midnight as ‘AM’.
%P like ‘%p’, except lower case. This is a GNU extension.
%r locale’s 12-hour clock time (e.g., ‘12:52:43 PM’)
%R 24-hour hour and minute. Same as ‘%H:%M’ (e.g., 12:52)
%s seconds since the epoch, i.e., since 1970-01-01 00:00:00 UTC. Leap seconds are not counted unless leap second support is available.
%S second (‘00’...‘60’). This may be ‘60’ if leap seconds are supported.
%T 24-hour hour, minute, and second. Same as ‘%H:%M:%S’, (e.g., 13:00:54)
%X locale’s time representation (e.g., ‘01:03:11 PM’)
%z RFC 2822/ISO 8601 style numeric time zone (e.g., ‘-0600’ or ‘+0530’), or nothing if no time zone is determinable. This value reflects the numeric time zone appropriate for the current time. Time zone is applied using the time zone rules specified by the ‘TZ’ environment variable. The time (and optionally, the time zone rules) can be overridden by the ‘--date’ option.
%:z RFC 3339/ISO 8601 style numeric time zone with ‘:’ (e.g., ‘-06:00’ or ‘+05:30’), or nothing if no time zone is determinable. This is a GNU extension.
%::z Numeric time zone to the nearest second with ‘:’ (e.g., ‘+05:30:00’)
%:::z locale’s time representation (e.g., ‘01:03:11 PM’)
%Z alphabetic time zone abbreviation (e.g., ‘EDT’ or ‘IST’), or nothing if no time zone is determinable.
%:::z locale’s time representation (e.g., ‘01:03:11 PM’)
%:::z locale’s time representation (e.g., ‘01:03:11 PM’)
%:::z locale’s time representation (e.g., ‘01:03:11 PM’)
-f, --file=DATEFILE like --date; once for each line of DATEFILE

Date conversion specifiers: %[aAbBcCdDeFgGhjmuUVwWxyY]

Date Specifier Description
%a locale’s abbreviated weekday name (e.g., ‘Sun’)
%A locale’s full weekday name, variable length (e.g., ‘Sunday’)
%b locale’s abbreviated month name (e.g., ‘Jan’)
%B locale’s full month name, variable length (e.g., ‘January’)
%c locale’s date and time (e.g., ‘Wed 05 Aug 2020 02:39:00 PM IST’)
%C century.This is like ‘%Y’, except the last two digits are omitted. For example, it is ‘20’ if ‘%Y’ is ‘2000’.
%d day of month (e.g., ‘05’)
%D date; same as ‘%m/%d/%y’ (e.g., ‘08/05/20’)
%e day of month, space padded; same as ‘%_d’
%F full date in ISO 8601 format; same as ‘%Y-%m-%d’. This is a good choice for a date format, as it is standard and is easy to sort in the usual case where years are in the range 0000...9999. (e.g., ‘2020-08-05’)
%g year corresponding to the ISO week number, but without the century (range ‘00’ through ‘99’). This has the same format and value as ‘%y’, except that if the ISO week number (see ‘%V’) belongs to the previous or next year, that year is used instead.
%h same as ‘%b’
%j day of year (‘001’...‘366’)
%m month (‘01’...‘12’)
%u day of week (‘1’...‘7’) with ‘1’ corresponding to Monday
%U week number of year, with Sunday as the first day of the week (‘00’...‘53’). Days in a new year preceding the first Sunday are in week zero.
%V ISO week number, that is, the week number of year, with Monday as the first day of the week (‘01’...‘53’). If the week containing January 1 has four or more days in the new year, then it is considered week 1; otherwise, it is week 53 of the previous year, and the next week is week 1.
%w day of week (‘0’...‘6’) with 0 corresponding to Sunday
%W week number of year, with Monday as first day of week (‘00’...‘53’). Days in a new year preceding the first Monday are in week zero.
%x locale’s date representation (e.g., ‘08/05/2020’)
%y last two digits of year (‘00’...‘99’)
%Y year. This is normally at least four characters,
% week number of year, with Monday as first day of week (‘00’...‘53’). Days in a new year preceding the first Monday are in week zero.
%w

Examples

1. Display date with no options or default format

$ date
Wed Aug  5 15:02:59 IST 2020

2. Display universal date and time

$ date -u
Wed Aug  5 09:35:18 UTC 2020

3. Display a given string in date format. Note, this does not affect the systems' actual date and time value.

$ date --date=20200810
Mon Aug 10 00:00:00 IST 2020

4. To display current date and time.

$ date --date='now'
Wed Apr 14 13:51:45 IST 2021

5. To print the date of the day before yesterday:

$ date --date='2 days ago'
Mon Apr 12 13:50:15 IST 2021

6. To print the date next monday.

$ date --date='next monday'
Mon Apr 19 00:00:00 IST 2021

7. To display the date 84 days ago.

$ date --date='84 days ago'
Wed Jan 20 13:56:33 IST 2021     

8. To display the date 2 months ago.

$ date --date='2 months ago'
Fri Jun  5 15:55:13 IST 2020

9. To print the date of the day three months and two days hence (from today).

$ date --date='3 months 2 day'
Fri Jul 16 14:00:59 IST 2021

10. Use --file option to show date mentioned on each line of the file in the DATEFILE. Let us have a DATEFILE as datefile.txt

$ cat >> datefile.txt
next month
Sept 23, 2020
today
next monday
Ctrl+D

$ cat datefile.txt 
next month
Sept 23, 2020
today
next monday

$ date --file=datefile.txt 
Sat Sep  5 16:08:05 IST 2020
Wed Sep 23 00:00:00 IST 2020
Wed Aug  5 16:08:05 IST 2020
Mon Aug 10 00:00:00 IST 2020

11. To print the day (weekday) of Christmas in the current year:

$ date --date='25 Dec' +%a
Sat 

12. To print the current weekday, full month name and the day of the month:

$ date '+%a, %B %d'
Wed, April 14
Advertisements