Display or set date and time

Green
options
not BSD
not mac os

java Script

date [-nu] [-r file] [ +format]

date [-jnRu] [-d ¬ datestring| --date= datestring] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]]
             [-f | fmt date | --file=fileOfDates
      [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]

date -d n -t minutes_west 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:
export TODAY=`date +%y%m%d`
quotes are unneeded as format does not include space etc
echo $TODAY
221231
export yymmdd=`date +%y%m%d` echo $yymmdd
130915
export LOG=`date +log/%y%m%d.%H%M` echo $LOG
log/211231.1859

myscript1 >> $LOG
myscript2 >> $LOG

export title=`date +"Report Date: %m/%d %H:%M"` Report Date: 07/20 18:56
echo `date "+%Y-%m-%d %H:%M:%S"` 2018-12-31 23:59:59 (Similar to linux syslog formats.)
touch `date +"%Y-%m-%d %H:%M:%S"` creates files named yy-mm-dd and hh:mm:ss
I forget why this is interesting! .

format codes
(arranged by function, not alphabetically, composite formats)

   N.B. If used in crontab command fields, % must be escaped.

locale: order of date items vary
as well as spellings 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 (0112)
to blank pad ie ' 1, 2, 3' vs '01,02,03' ?? ) sed s/0([[:digit:]])/ $1/
%b abbreviated month (JanDec) %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)
%g 2-digit year corresponding to the %V week number %G 4-digit
%F %Y-%m-%d (2013-02-24)
%D mm/dd/yy %x mm/dd/yy
hours
%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
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)
%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
%c date and time
(Sat Nov 04 12:02:33 EST 1989 )
%j day of year (001…366)
%u day of week (1…7); 7 represents Sunday mtwtfss
%w                    (0…6); 0 represents Sunday smtwtfs
%a abbreviated weekday (Sun…Sat)
%A full weekday, variable length (Sunday…Saturday)
%U week number Sunday as first day of week (00…53)
%W week number Monday as first day of week (00…53)    %V (01…53)
%z -zzzz RFC-822 style numeric timezone (-0400)
%:z +hh:mm numeric timezone ( -04:00)
%::z +hh:mm:ss numeric time zone ( -04:00:00)
%:::z numeric time zone with : to necessary precision (-04, +05:30)
%Z time zone (EDT) nothing if no time zone is determinable
$TZ timezone, unless overridden by command line parameters.
Default: the setting from /etc/localtime is used.
on macBookPro:
     > hexdump -e  '"%_p"' -s 0x4DB -n 7   /etc/localtime
     EDT.EST

On Real-World-Systems.com
     hexdump -e  '"%_p"'    /etc/localtime |tail -2
     .MST7MDT,M3.2.0,M1*

     > echo $TZ
     America/Boise
     > date
     Wed Mar 27 18:36:50 MDT 2019

     export TZ=US/Eastern 
     > echo $TZ
     US/Eastern
     > date
     Wed Mar 27 20:37:00 EDT 2019


%s seconds since 00:00:00 1970-01-01 UTC i.e. epoch
To convert %s value like 1212642879
 use date -d "1970-01-01 1212642879 sec utc" Not mac

  1264434947 was Mon Jan 25 10:55:57 EST 2010
  1402410634 was Tue Jun 10 10:29:44 EDT 2014
Example usage:
export login_s=`date +%s`  # include this in .profile
#      ~/.bash_logout
echo " $(((`date +%s` - $login_s)/60))  minutes"
sleep 3 
literals: %n newline ; %% percent; %t horizontal tab

By default, date pads numeric fields with zeroes.

GNU (not BSD) date recognizes modifiers between % and a numeric:
- (hyphen) do not pad the field           _ (underscore) pad the field with spaces
 examples:

%_H == %k       %-m/%-d/%y 1/2/50

Options

-d string
-de=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 2010 
To convert seconds (for example 1212642879) use:
> date -d "1970-01-01 1212642879 sec utc"
Thu Jun 5 01:14:39 EDT 2008

-e=datefile like -de once for each line of datefile
BSD(Mac OS X):
-f fmt_str idate [ +fmt] Output idate
Default 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. Dec 31 19:00:00 EST 1969 i.e. 01/01/70 00:00:00)

-r secondsSinceEpochi Use secondsSinceEpoch instead of current time
-r file
--reference=file
output modification date/time of file
         > date -r 0test 
         Thu Aug 19 18:05:09 EDT 2010

         > date -r arp.1.html +"%y%m%d_%H%M"
         140630_1810
-v [±]value​m|d|H|M|S|w
-v{±]name
Output the current date set as per value. Suffix is required (without preceeding space) and for y the value must be in the range of 80-38 or 1980-2038, otherwise return code will be 1.
name is a case in-sensitive, 3 character or longer week_day or month .

If value is preceded with a + or - the date is adjusted forwards or backwards.
If a name is used with a sign, the date will be adjusted forwards (or backwards) to the next (previous) date that matches the given week day or month. If the given week day or month is the same as the current one no adjustment is made.

Attempting to set or adjust a date to an invalid day fails with return code of 1. For example: -v 31d, for months having less than 31 days.
To minimize this problem, set the minutes first then the day, for example: -v 12m -v 31d Adjusting by months is ambiguous because months have varying numbers of days.
Adjusting the month on the 31st, if the target month is shorter than the present one, will result in using the last day of the target month.
For example, using -v +1m on May 31 will adjust the date to June 30,

Daylight Saving Time

Daylight savings time considerations are ignored. when a date is set to a value or adjusted in units greater than hours, Daylight saving time is honor 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
If the date is adjusted to a value that occurs twice (for example October 29, 01:30 2000), the resulting timezone will be set so that the date matches the earlier of the two times. Multiple -ts are permitted.

-u
--utc
--universal
[MMDDhhmm[[CC]YY][.ss]]
Sun Jun 27 21:56:05 UTC 2021
aka GMT ex: Sat Feb 5 14:49:42 GMT 2005
-I[precision]
--iso-8601[=precision]
output in ISO 8601 format.
precision : date | hours | minutes | seconds | ns
> date --iso-8601                   2010-08-23
> date --iso-8601=hours             2010-08-23T21-0400
> date --iso-8601=minutes           2010-08-23T21:05-0400
> date --iso-8601=seconds           2010-08-23T21:05:53-0400 
> date --iso-8601=ns                2010-08-23T21:05:53,632672872-04:00
-R
--rfc-822
output RFC-822 compliant date string
                                     Sun, 23 Aug 2010 21:53:18 -0400
-s string
--set=string
set the system the clock to string
Example: date -s "2010-12-31 23:59:00"
-d dst Set kernel value for daylight saving time. 
-n No synchroniztion of clocks on groups of machines using timed.
 Default: if timed is running, date will set the time on all of the machines in the local group,
-n inhibites that.
--help display help and exit
--version output version information and exit

see also

strftime

java script functions

From w3schools.com
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,dd
since 00:00 1/1/1970
according to universal time
1333065600000