date

Display or change the date.

Syntax
      date [option]... [+Format]
  
      date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]

'date' with no arguments prints the current time and date, in the format of the %c directive (described below).

If given an argument that starts with a +, date prints the current time and date (or the time and date specified by the --date option, see below) in the format defined by that argument, which is the same as in the strftime function.

Except for directives, which start with %, characters in the format string are printed unchanged. The directives are described below.

Options:
  -d, --date=String         Display time described by String, instead of 'now'
                            this can be in almost any common format.  
                            It can contain month names, timezones, 'am' and 'pm',
                            'yesterday', 'ago', 'next', etc.

  -f, --file=DateFile       like --date once for each line of DateFile
                            If DateFile is '-', use standard input.  This is
                            useful when you have many dates to process, 
                            because the system overhead of starting up the
                            'date' executable many times can be considerable.

  -I, --iso-8601[=Timespec] Output an ISO 8601 compliant date/time string., '%Y-%m-%d'.
                            Timespec='date' (or missing) for date only,
                            'hours', 'minutes', or 'seconds' for date and
                            time to the indicated precision.
                            If showing any time terms, then include the time zone 
                            using the format '%z'.  If '--utc' is also specified, 
                            use '%Z' in place of '%z'.

  -r, --reference=File      Display the last modification time of File

  -R, --rfc-822             Output RFC-822 compliant date string
                            Example: Mon, 19 Nov 2012 12:44:56 -0600
                            deprecated in favor of  --rfc-email

      --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, --rfc-email           Output date and time in RFC 5322 format.
                            Example: Mon, 14 Aug 2006 02:34:56 -0600

  -s, --set=String          Set time described by String (see -d above)

  -u, --utc, --universal    Print or set Coordinated Universal Time

      --help                Display this help and exit
      --version             output version information and exit

Format controls the output as follows.

  %%   a literal %
  %a   locale's abbreviated weekday name (Sun..Sat)
  %A   locale's full weekday name, variable length (Sunday..Saturday)
  %b   locale's abbreviated month name (Jan..Dec)
  %B   locale's full month name, variable length (January..December)
  %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 (01..31)
  %D   Date (mm/dd/yy) same as %m/%d/%y
  %e   Day of month, blank padded ( 1..31); same as %_d
  %F   Full date; like %+4Y-%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, locale's abbreviated month name (Jan..Dec)
  %H   Hour :24 hour(00..23)
  %I   Hour :12 hour(01..12)
  %j   day of year (001..366)
  %k   Hour, space padded ( 0..23); same as %_H
  %l   Hour, space padded ( 1..12); same as %_I
  %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
  %q   Quarter of year (1..4)

  %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, Unix epoch time (a GNU extension)
       This is often used to generate website URLs.
       Defined by a localtime system call. It isn't changed by the '--date' option.
  %S   second (00..60)
  %t   a horizontal tab
  %T   Time, 24-hour (hh:mm:ss) 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)
       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. Similar to ISO 8601 (but not 100% compliant as not locale aware.)

  %w   Day of week (0..6);  0 represents Sunday
  %W   Week number of year with Monday as first day of week (00..53)
  %x   Locale's date representation (mm/dd/yy)
  %X   Locale's time representation (%H:%M:%S)
  %y   Last two digits of year (00..99)
  %Y   Year (1970...)
  %z   RFC-822 style numeric timezone (-0500) (a nonstandard extension)
       +hhmm numeric time zone (e.g., -0400)
       This value reflects the current time zone. Is not changed  by the --date option.
  %: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   Time offset from UTC (-07) This generally consists of Time Zone+DST
       Is not changed by the --date option.

By default, date pads numeric fields with zeroes. GNU date recognizes the following modifiers between % and a numeric directive.

- (hyphen) do not pad the field; useful if the output is intended for human consumption.
_ (underscore) pad the field with spaces; useful if you need a fixed number of characters in the output, but zeroes are too distracting.
0 (zero) pad with zeros
+ pad with zeros, and put '+' before future years with >4 digits
^ use upper case if possible
# use opposite case if possible

After any flags comes an optional field width, as a decimal number; then an optional modifier, which is either E to use the locale's alternate representations if available, or O to use the locale's alternate numeric symbols if available.

The - and _ are GNU extensions. Here is an example illustrating the differences:

     date +%d/%m -d "Feb 1"
     => 01/02
     date +%-d/%-m -d "Feb 1"
     => 1/2
     date +%_d/%_m -d "Feb 1"
     =>  1/ 2

Here are the same format codes in categories:

     Date:
       %D    Date in mm/dd/yy format (06/24/13)
       %x    Date in standard format for locale (09/24/13 for English-US)
        
     Year:
       %C   Century (20 for 2015)
       %Y   Year in 4-digit format (2015)
       %y   Year in 2-digit format (14)
       %G   Same as 'Y'
       %g   Same as 'y'
        
     Month: 
       %b   Month name - abbreviated (Jan)
       %B   Month name - full (January)
       %h   Same as 'b'
       %m   Month number (09)
        
     Week:     
       %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   week number of year with Monday as first day of week (00..53)
        
     Day:
       %a   Day of the week - abbreviated name (Mon)
       %A   Day of the week - full name (Monday)
       %u   Day of the week - number (Monday = 1)
       %d   Day of the month - 2 digits (05)
       %e   Day of the month - digit preceded by a space ( 5)
       %j   Day of the year - (1-366)
       %u   day of week (1..7); 1 is Monday
       %w   Same as 'u'
        
     Time:
       %p   AM or PM
       %r   Time in 12-hour format (09:15:36 AM)
       %R   Time in 24-hour format - no seconds (17:45)
       %T   Time in 24 hour format (17:45:52)
       %X   Same as 'T'
       %Z   Time offset from UTC (-07) This generally consists of Time Zone+DST
        
     Hour: 
       %H   Hour in 24-hour format (17)
       %I   Hour in 12 hour format (05)
       %k   Same as 'H'
       %l   Same as 'I' (Upper-case I = Lower-case L)
        
     Minutes & Seconds:
       %M   Minutes (35)
       %S   Seconds (05)
       %s   Seconds elapsed since January 1, 1970 00:00:00 UTC (Unix time)

Setting the time

If given an argument that does not start with +, date sets the system clock to the time and date specified by that argument (as described below). You must have appropriate privileges to set the system clock. The --date and --set options can not be used with such an argument. The --universal option can be used with such an argument to indicate that the specified time and date are relative to Coordinated Universal Time rather than to the local time zone.

The argument must consist entirely of digits, which have the following meaning:

 MM     month
 DD     day within month
 HH     hour
 MM     minute
 CC     first two digits of year (optional)
 YY     last two digits of year (optional)
 SS     second (optional) 
 
 The '--set' option also sets the system clock; see the examples below.

Examples

Print the date of the day before yesterday:

$ date --date='2 days ago'

Rename a file with the current date and time:

$ STAMPME=$HOME/demo_file_$(date +%Y%m%d-%H%M).txt
$ mv $HOME/demo_file $STAMPME

Show the time on the west coast of the US (use tzselect(1) to find TZ):

$ TZ='America/Los_Angeles'
$ date

Show the local date/time for 9AM next Friday on the west coast of the US

$ date --date='TZ="America/Los_Angeles" 09:00 next Fri'

Print the date of the day three months and one day hence:

$ date --date='3 months 1 day'

Print the day of year of Christmas in the current year:

$ date --date='25 Dec' +%j

Print the current full month name and the day of the month:

$ date '+%B %d'
Note that the '%d' expands to a zero-padded two-digit field, for example:
$ date -d 1may '+%B %d'  will print 'May 01'.

Print a date without the leading zero for one-digit days of the month, you can use the (GNU extension) '-' modifier to suppress the padding altogether:

$ date -d=1may '+%B %-d'

Print the current date and time in the format required by many non-GNU versions of 'date' when setting the system clock:
$ date +%m%d%H%M%Y.%S

Set the system date and time:

$ date --set="2012-6-29 11:59 AM"

Set the system clock forward by two minutes:

$ date --set='+2 minutes'

Print the date as an ISO 8601 compliant date/time string:

$ date -I

To convert a date string to the number of seconds since the epoch 1970-01-01 00:00:00 GMT (Unix time), use the '--date' option with the '%s' format. That can be useful in sorting and/or graphing /or comparing data by date. The following command outputs the number of the seconds since the epoch for the time one second later than the epoch, but in time zone five hours later (Cambridge,Massachusetts), thus a total of five hours and one second after the epoch:

$ date --date='2000-01-01 00:00:01 UTC +5 hours' +%s
946706400

Suppose you had not specified time zone information in the example above. Then, date would have used your computer's idea of the time zone (and DST) when interpreting the string. Here's what you would get if you were in Greenwich, England:

# local time zone will be used
$ date --date='2000-01-01 00:00:01' +%s
946684800

Seconds since the 1970 epoch can be useful when sorting or graphing dated data . But to convert a number of seconds back to a more readable date, use a command like:

$ date -d '1970-01-01 946684800 sec' +"%Y-%m-%d %T %z"
2000-01-01 00:00:00 +0000

“Carpe Diem - Seize the day” ~ Horace

Related linux commands

cal - Display a calendar.
crontab - Schedule a command to run at a later time.
time - Measure Program Resource Use.
times - User and system times.
touch - Change file timestamps.
Standard date and time notation - YYYY-MM-DD
Unixtimestamp.com - online converter.
The Time Zone Database - IANA.
Equivalent Windows command: DATE - Display or set the date + TIME - Display or set the system time.


 
Copyright © 1999-2024 SS64.com
Some rights reserved