date

This is a wiki page. Be bold and improve it!

If you have any questions about the content on this page, don't hesitate to open a new ticket and we'll do our best to assist you.

Usage

Basic usage

See man date

Simplest use:

$ date "+%Y"
2016

within scripts

To date-stamp or time-stamp your files, the following may be useful within scripts:

date +%F          # yyyy-mm-dd, e.g 2016-02-10
date +%H-%M-%S    # hh-mm-ss, e.g. 15-26-10
date +%F-%H-%M-%S # Combines the two above.
date +%s          # Unix timestamp, e.g. 1455089288
date +%s.%N       # If necessary, add nanoseconds, e.g. 1455089359.711595818.

Format

The man page has the format codes listed in alphabetical order. Here is a more convenient ordering by time frame:

Pre-formatted

%c     locale's date and time (e.g., Thu Mar  3 23:05:25 2005)
%D     date; same as %m/%d/%y
%F     full date; same as %Y-%m-%d
%r     locale's 12-hour clock time (e.g., 11:11:04 PM)
%R     24-hour hour and minute; same as %H:%M
%T     time; same as %H:%M:%S
%x     locale's date representation (e.g., 12/31/99)
%X     locale's time representation (e.g., 23:13:48)

century, year

%C     century; like %Y, except omit last two digits (e.g., 20)
%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
%y     last two digits of year (00..99)
%Y     year, four digits (e.g. 2016)

month

%b     locale's abbreviated month name (e.g., Jan)
%h     same as %b
%B     locale's full month name (e.g., January)
%m     month (01..12)

week

%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
%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)

For week days, see below.

day of the year, month, week

Day of the year:

%j     day of year (001..366)

Day of the month:

%d     day of month (e.g, 01)
%e     day of month, space padded; same as %_d

Day of the week:

%a     locale's abbreviated weekday name (e.g., Sun)
%A     locale's full weekday name (e.g., Sunday)
%u     day of week (1..7); 1 is Monday
%w     day of week (0..6); 0 is Sunday

hour, minutes

hour:

%H     hour (00..23)
%I     hour (01..12)
%k     hour ( 0..23)
%l     hour ( 1..12)
%p     locale's equivalent of either AM or PM; blank if not known
%P     like %p, but lower case

minute:

%M     minute (00..59)

seconds, nanoseconds, timestamp

%S     second (00..60)
%s     seconds since 1970-01-01 00:00:00 UTC
%N     nanoseconds (000000000..999999999)

timezones

%z     +hhmm numeric timezone (e.g., -0400)
%:z    +hh:mm numeric timezone (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)

formatting characters

%%     a literal %
%n     a newline
%t     a tab

By default, date pads numeric fields with zeroes. The following optional flags may follow `%':

-      (hyphen) do not pad the field
_      (underscore) pad with spaces
0      (zero) pad with zeros
^      use upper case if possible
#      use opposite case if possible

Errors

invalid date

$ date "%Y"
date: invalid date `%Y'

Do not forget the + sign: date "+%Y".

$ date 1467237996                                                                                
date: invalid date `1467237996'                              
$ date -d @1467237996                                                                                        
Thu Jun 30 06:06:36 CST 2016                                

This use of date is only documented in the 'examples' section of the man page for date.

extra operand

$ date now "+%Y"
date: extra operand `+%Y'
Try `date --help' for more information.

By default date displays the current date.
Simply use: date "+%Y"