Display or set date and time
options not BSD not mac os |
date [-jnRu] [-d ¬ datestring| --date= datestring] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] …
date -d n -t minutes_west
date [-nu]
[-r file] [ +format]
[-f | fmt date | --file=fileOfDates
[[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]
date [[[[[cc]yy]mm]dd]hh]mm[.ss] # set system time
The default format is: | "%a %b %e %H:%M:%S %Z %Y"
|
| sample output: | Sat Dec 31 18:09:28 EST 2021 |
Useful forms: date "+%y-%m-%d_%H:%M:%S $0 started with $*" >> %y%m%d-%H%M.log
| |||||||||||||
export TODAY=`date +%y%m%d`quotes are unneeded as format does not include space etc | echo $TODAY | ||||||||||||
format codes(arranged by function, not alphabetically, composite formats)
N.B. If used in | Items which vary by localeOrder of date items varySpellings as per language. Examples: LC_ALL=C > date Wed Jun 22 13:18:10 EDT 2016 > export LC_ALL=es_ES # use Spanish (EspaƱol) > date miƩrcoles, 22 de junio de 2016, 13:39:34 EDT | |||||||||||||||
%m mm month (01…12) to blank pad ie ' 1, 2, 3' vs '01,02,03' ?? ) sed s/0([[:digit:]])/ $1/ %b abbreviated month (Jan…Dec)
%h (same as:%b) %B full month, variable length (January…December) | ||||||||||||||||
%d dd (01…31), %e ( ' 1'…'31') | ||||||||||||||||
%C cc century (00…99)
%y yy two digit year (00…99)%Y ccyy year (1970)
| ||||||||||||||||
%F %Y-%m-%d (2013-02-24) | ||||||||||||||||
%y%m%d (241231) | ||||||||||||||||
%D mm/dd/yy | %x mm/dd/yy
| |||||||||||||||
%g 2-digit year corresponding to the %V week number %G 4-digit | ||||||||||||||||
Time%I (01…12) %H (00…23) %l† (1…12) %k (0…23) | ||||||||||||||||
%p AM or PM upper case (blank in many locales)
%P am or pm lower case | ||||||||||||||||
%M MM minutes (00…59) | ||||||||||||||||
%S SS second (00…60) The 60 is necessary to accommodate a leap second | ||||||||||||||||
| %H%M%S 235958 | ||||||||||||||||
24-hour: %R (23:59), %T HH:mm:ss ( 11:53:41 )12-hour: %r (11:26:20 AM )%X %H:%M:%S (11:56:05 AM)
| ||||||||||||||||
| %c date and time (Sat Nov 04 12:02:33 EST 1989 ) | ||||||||||||||||
%N nanoseconds
†
since last second (000000000..999999999) (on mac OS use gdate )Use sed to restrict the output to milliseconds: date +%S.%N|sed "s/......$//" 26.123
| ||||||||||||||||
-d string |
Display time described by string Including some "humand readable" strings like last Thursday and last month
date -d "2010/12/31" Fri Dec 31 00:00:00 EST 2010 date -d "12/31/2010" Fri Dec 31 00:00:00 EST 2010 date -d "2010-12-31 23:58:57" Fri Dec 31 23:58:57 EST 2010 date -d "23:58:57 12/31/2010" Fri Dec 31 23:58:57 EST 2010 date -d "12/31/2010 23:58:57" Fri Dec 31 23:58:57 EST 2010 date -d "12/31/2010 23 sec" Fri Dec 31 00:00:23 EST 2010To convert seconds (for example 1212642879) use:
| ||||||||||||||||||||
-e=datefile | like -de once for each line of datefile
| ||||||||||||||||||||
| BSD(Mac OS X): | |||||||||||||||||||||
-f fmt_str idate [ +fmt] | Output idateDefault fmt_str is [[[mm]dd]HH]MM[[cc]yy][.ss]
| ||||||||||||||||||||
-j with -f | Don't set
date and time seconds from the Epoch. ( i.e. | ||||||||||||||||||||
-r secondsSinceEpochi |
Daylight Saving TimeDaylight savings time considerations are ignored when a date is set/adjusted to a value or adjusted in units greater than hours. Daylight saving time is honored with Adjustments in units of hours or less .For example: If the current date is March 26, 00:30 and the DST adjustment means the clock goes forward at 01:00 to 02:00, using -v +1H will adjust the date to March 26, 02:30. if the date is October 29, 00:30 and the DST adjustment means that the clock goes back at 02:00 to 01:00, -v +3H is necessary to reach October 29, 02:30.
If the date is adjusted to a value that does not exist (due to day light savings time, for example March 26, 01:30 BST 2000
in the Europe/London timezone), the date will be adjusted forwards in units of one hour until it reaches a valid time |
| according to UTC (aka GMT) Max 00 1F FF FF FF FF FF FF aka 9,007,199,254,740,991
date object is local
getMonth() | 0-11
| getUTCMonth()
| getDate() | day of the month ( 1-31)
| getUTCDate()
| getFullYear() | year yyyy
| getUTCFullYear()
| getHours() | 0-23
| getUTCHours()
| getMinutes() | 0-59
| getUTCMinutes()
| getSeconds() | 0-59
| getUTCSeconds()
| getMilliseconds() | 0-999
| getUTCMilliseconds()
| getDay() | of the week ( 0-6)
| getUTCDay()
| getTime() | milliseconds since 1/1/1970 00:00
| getTimezoneOffset() | minutes between UTC and local
| getYear() | Deprecated. Use the getFullYear() method
| parse() | number of milliseconds since 1/1/1970 00:00 of date string
| set portions of a date object
| setFullYear() | YYYY
| setUTCFullYear()
| setMonth() |
| setUTCMonth()
| setDate() | day of the month
| setUTCDate()
| setHours() |
| setUTCHours()
| setMinutes() |
| setUTCMinutes()
| setSeconds() |
| setUTCSeconds()
| setMilliseconds() |
| setUTCMilliseconds()
| setTime() | Sets a date and time | by adding or subtracting milliseconds to/from 1/1/1970 00:00 setYear() | Deprecated. Use the setFullYear() method
| formatting to string
| toString() | Sat Aug 04 2012 17:09:15 GMT-0400 (EDT)
| toLocaleString() | Sat Aug 4 17:08:41 2012 | Really! YYYY last toISOString()
| 2012-08-04T21:03:51.449Z
| toJSON() | JavaScript Object Notation 2012-08-04T20:27:42.419Z
| toUTCString() | Sat, 04 Aug 2012 21:32:18 GMT
| toDateString() | Sat Aug 04 2012
| toLocaleDateString() | 08/04/2012
| toTimeString() | 17:14:24 GMT-0400 (EDT)
| toLocaleTimeString() | 17:06:00
| toGMTString() | Deprecated. Use the toUTCString() method
| valueOf() | primitive value of a Date object | 1344116221551 UTC(yyyy,mm,dd) | number of milliseconds in | yyyy,mm,ddsince 00:00 1/1/1970 according to universal time 1333065600000 | ||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|