date
Here are a few examples. Also see the documentation for the -d option in the previous section.
date --date='2 days ago'
date --date='3 months 1 day'
date --date='25 Dec' +%j
date '+%B %d'
But this may not be what you want because for the first nine days of the month, the ‘%d’ expands to a zero-padded two-digit field, for example ‘date -d 1may '+%B %d'’ will print ‘May 01’.
date -d 1may '+%B %-d'
date
when setting the system clock:
date +%m%d%H%M%Y.%S
date --set='+2 minutes'
Tue, 09 Jul 2020 19:00:37 -0400
date --date='1970-01-01 00:02:00 +0000' +%s 120
To convert a date string from one time zone from to another to, specify ‘TZ="from"’ in the environment and ‘TZ="to"’ in the --date option. See Specifying time zone rules. For example:
TZ="Asia/Tokyo" date --date='TZ="America/New_York" 2023-05-07 12:23' Mon May 8 01:23:00 JST 2023
If you do not specify time zone information in the date string,
date
uses your computer’s idea of the time zone when
interpreting the string. For example, if your computer’s time zone is
that of Cambridge, Massachusetts, which was then 5 hours (i.e., 18,000
seconds) behind UTC:
# local time zone used date --date='1970-01-01 00:02:00' +%s 18120
date --date='2020-01-01 UTC' +%s 1577836800
An alternative is to use the --utc (-u) option. Then you may omit ‘UTC’ from the date string. Although this produces the same result for ‘%s’ and many other format sequences, with a time zone offset different from zero, it would give a different result for zone-dependent formats like ‘%z’.
date -u --date=2020-07-21 +%s 1595289600
To convert such an unwieldy number of seconds back to a more readable form, use a command like this:
date -d @1595289600 +"%F %T %z" 2020-07-20 20:00:00 -0400
Often it is better to output UTC-relative date and time:
date -u -d @1595289600 +"%F %T %z" 2020-07-21 00:00:00 +0000
Here is how the two kinds of systems handle the leap second at the end of the year 2016:
# Typical systems ignore leap seconds: date --date='2016-12-31 23:59:59 +0000' +%s 1483228799 date --date='2016-12-31 23:59:60 +0000' +%s date: invalid date '2016-12-31 23:59:60 +0000' date --date='2017-01-01 00:00:00 +0000' +%s 1483228800
# Atypical systems count leap seconds: date --date='2016-12-31 23:59:59 +0000' +%s 1483228825 date --date='2016-12-31 23:59:60 +0000' +%s 1483228826 date --date='2017-01-01 00:00:00 +0000' +%s 1483228827