date

From RaySoft

The Unix date command displays and sets the time and date of the system clock. Setting the clock is restricted to the superuser.[1]

Documentation

Request for Comments (RFC)

Syntax

date [+FORMAT] [PARAMETER ...]

Parameters

-d STRING, --date=STRING
Display time described by STRING, not now.
-R, --rfc-2822
Output date and time in RFC 2822 format. Example: Mon, 07 Aug 2006 12:34:56 -0600
--rfc-3339=TIMESPEC
Output date and time in RFC 3339 format. TIMESPEC='date', 'seconds', or 'ns' for date and time to the indicated precision. Date and time components are separated by a single space: 2006-08-07 12:34:56-06:00
-u, --utc, --universal
Print or set Coordinated Universal Time (UTC).

Format

%B
Locale's full month name (e.g. January)
%d
Day of month (e.g. 01)
%F
Full date; same as %Y-%m-%d
%H
Hour (00..23)
%M
Minute (00..59)
%m
Month (01..12)
%s
Seconds since 1970-01-01 00:00:00 UTC
%S
Second (00..60)
%T
Time; same as %H:%M:%S
%u
Day of week (1..7); 1 is Monday
%V
ISO week number, with Monday as first day of week (01..53)
%Y
Year (e.g. 2011)
%Z
Alphabetic time zone abbreviation (e.g., EDT)
- (Hyphen)
Do not pad the field.
_ (Underscore)
Pad with spaces.

Examples

Show the current time and date
date '+%Y-%m-%d %H:%M:%S'
date '+%F %T'

Output:

2011-08-18 10:31:57
2011-08-18 10:31:57
Show the seconds since 1970-01-01 00:00:00 UTC when the UNIX time started
date '+%s' --utc --date='1970-01-01 00:00:01'
date '+%s' --utc

Output:

1
1502181583
Calculate dates
date '+%F' --date='4 years ago 2 days ago'
date '+%F'
date '+%F' --date='3 months 1 day'

Output:

2007-12-27
2011-12-29
2012-03-30
Work with different time zones
TZ='US/Hawaii'        date '+%T %Z %:z'
TZ='America/New_York' date '+%T %Z %:z'
TZ='Universal'        date '+%T %Z %:z'
TZ='Europe/London'    date '+%T %Z %:z'
TZ='Europe/Zurich'    date '+%T %Z %:z'
TZ='Australia/Sydney' date '+%T %Z %:z'

Output:

01:07:17 HST -10:00
07:07:17 EDT -04:00
11:07:17 UTC +00:00
12:07:17 BST +01:00
13:07:17 CEST +02:00
21:07:17 AEST +10:00
Work with different languages
LC_ALL='en_US' date '+%A, %-d. %B %Y'
LC_ALL='de_CH' date '+%A, %-d. %B %Y'
LC_ALL='fr_FR' date '+%A, %-d. %B %Y'
LC_ALL='ru_RU' date '+%A, %-d. %B %Y'

Output:

Monday, 14. June 2021
Montag, 14. Juni 2021
Lundi, 14. juin 2021
понедельник, 14. июня 2021
Get the last day of the current month
date '+%-m' --date="$(date '+%Y-%m')-01 +1 month -1 day"

Output:

30
Calculate the time difference between two locations in hours
echo $((
  (
    $(date '+%s' --date='TZ="Europe/Zurich" 12:00:00')
    - $(date '+%s' --date='TZ="Australia/Sydney" 12:00:00')
  ) / 3600
))

Output:

8
Calculate the time difference between a location and UTC in hours
echo $((
  (
    $(date '+%s' --date='TZ="Europe/Zurich" 12:00:00')
    - $(date '+%s' --utc --date='12:00:00')
  ) / 3600
))

Output:

-2
Convert seconds in a human readable format
display_time() {
  local count=0

  local day=$(($1 / 60 / 60 / 24)) \
        hour=$(($1 / 60 / 60 % 24)) \
        minute=$(($1 / 60 % 60)) \
        second=$(($1 % 60))

  for unit in 'day' 'hour' 'minute' 'second'; do
    if [[ ${!unit} > 0 ]]; then
      if [[ ${count} > 0 ]]; then
        printf ', '
      fi

      printf '%d %s' ${!unit} "${unit}"

      if [[ ${!unit} > 1 ]]; then
        printf 's'
      fi

      ((count++))
    fi
  done

  echo
}

display_time 90061
display_time 180122

Output:

1 day, 1 hour, 1 minute, 1 second
2 days, 2 hours, 2 minutes, 2 seconds

References