Flag | Usage |
---|---|
(none) |
|
-d dateString --date dateString |
instruct date to work with dateString rather than with the current date+time. This is used mostly to
|
--iso-8601 --iso-8601=precision |
display date/time using the ISO 8601 format :
date; for precision in date hours minutes seconds ns; do echo -n "$precision : "; date --iso-8601="$precision"; done
Fri 11 Mar 2022 09:10:22 AM CET just date, for comparison date : 2022-03-11 default precision hours : 2022-03-11T09+01:00 up to the current hour minutes : 2022-03-11T09:10+01:00 up to the current hour:minute seconds : 2022-03-11T09:10:22+01:00 up to the current hour:minute:second ns : 2022-03-11T09:10:22,111460006+01:00 up to the current hour:minute:second,nanosecond |
-r file --reference file |
display the last modification time of file |
-s dateString --set dateString |
set time described by dateString :
date --set="Oct 31 10:52:40"
|
The --date='dateString' is a human-readable format such as next Thursday or 1 month ago. A date string may contain items indicating :
date Fri 19 Jul 2019 11:17:29 AM CEST date --date="today" same as above Fri 19 Jul 2019 11:17:44 AM CEST date --date="tomorrow" Sat 20 Jul 2019 11:17:54 AM CEST date --date="next monday" Mon 22 Jul 2019 12:00:00 AM CEST date; TZ=GMT date display current date+time in a different time zone Tue Feb 20 09:32:07 AM CET 2024 Tue Feb 20 08:32:07 AM GMT 2024
date --date="3 days" Mon 22 Jul 2019 11:22:13 AM CEST date --date="3 days ago" ago ... Tue 16 Jul 2019 11:22:19 AM CEST date --date="-3 days" and negative values compute dates in the past ... Tue 16 Jul 2019 11:22:25 AM CEST date --date="-3 days ago" unless you combine both Mon 22 Jul 2019 11:37:53 AM CEST date --date="-1 year month" Sun 19 Aug 2018 12:32:02 PM CEST +1 is implied for units without a number
date --date="thursday" Thu 25 Jul 2019 12:00:00 AM CEST date --date="this thursday" Thu 25 Jul 2019 12:00:00 AM CEST date --date="next thursday" Thu 25 Jul 2019 12:00:00 AM CEST date --date="friday" Fri 19 Jul 2019 12:00:00 AM CEST date --date="this friday" Fri 19 Jul 2019 12:00:00 AM CEST date --date="next friday" Fri 26 Jul 2019 12:00:00 AM CEST date --date="saturday" Sat 20 Jul 2019 12:00:00 AM CEST date --date="this saturday" Sat 20 Jul 2019 12:00:00 AM CEST date --date="next saturday" Sat 20 Jul 2019 12:00:00 AM CEST
date --date="next monday" Mon 22 Jul 2019 12:00:00 AM CEST date --date="1 monday" same as next Mon 22 Jul 2019 12:00:00 AM CEST date --date="2 monday" Mon 29 Jul 2019 12:00:00 AM CEST date --date="second monday" second is "1 second", not "the second from now" Mon 22 Jul 2019 12:00:01 AM CEST date --date="third monday" Mon 05 Aug 2019 12:00:00 AM CEST date --date="fourth monday" Mon 12 Aug 2019 12:00:00 AM CEST date --date="tenth monday" Mon 23 Sep 2019 12:00:00 AM CEST
+%F %H:%M:%S
'; startTime=$(date "$outputFormat"); endTime=$(date -d '+2years' "$outputFormat"); echo -e "start :\t$startTime\nend :\t$endTime"start : 2024-12-05 15:37:40 end : 2026-12-05 15:37:40
+%F %H:%M:%S.%N
'; startTime=$(date "$outputFormat"); endTime=$(date -d '+2years' "$outputFormat"); echo -e "start :\t$startTime\nend :\t$endTime"start : 2024-12-05 15:50:47.065296725 end : 2026-12-05 15:50:47.067093474which is milliseconds away from the exact "start +2years". This is because :
startTime
is "now"endTime
is based on "now" + a delay, but this "now" occurred a blink after the "now" of startTime
, hence the difference.+%F %H:%M:%S.%N %Z
'; currentDateTime=$(date "$outputFormat"); startTime=$(date -d "$currentDateTime" "$outputFormat"); endTime=$(date -d "$currentDateTime +2years" "$outputFormat"); echo -e "start :\t$startTime\nend :\t$endTime"start : 2024-12-05 16:07:50.159780462 CET end : 2026-12-05 16:07:50.159780462 CET
$currentDateTime
has no timezone field (%Z), the computed value for $endTime
is effectively after $startTime
, but not by the specified delay.less -p "^ *FORMAT "
' dateOutput | Format string | Usage |
---|---|---|
20170201 | date +%Y%m%d | compact representation of a date that can be used in a filename |
20181116_152852 | date +%Y%m%d_%H%M%S | |
2018-11-29 15:51:36 | date '+%F %H:%M:%S' | Quotes are necessary when the format string has a space, otherwise you'll get a date: extra operand '%H:%M:%S' error message.
%F is a synonymous of %Y-%m-%d
|
2024-04-25 |
|
|
2011-12-29_10-23-08 | date +%F_%H-%M-%S | (almost) same as above except the : changed to _ , which is better if this timestamp is part of a filename |
1014 | date +%H%M | at time format |
jeu., 28 nov. 2013 15:10:46 GMT | date +"%a, %d %b %Y %H:%M:%S GMT" | If-Modified-Since HTTP header date format |
1387202860 | date +%s | Unix timestamp, see examples |
Wed Mar 15 Fri Sep 1 |
date +"%a %b %-d" | git log date format
the extra
- prevents padding the day number with a leading 0 if <10.this format ignores the year, which may have unexpected results if the Git history has commits on the "same day" of distinct years (which happened to me with Wed Dec 12 of 2018 and 2012)
|
Fri Mar 13 17:35:03 2020 +0100 |
date +"%a %b %-d %H:%M:%S %Y %z" | git log long date format (depending on personal settings / aliases /...) |
CEST +0200 |
date +"%Z %z" | current timezone : alphabetic abbreviation, then numeric |
16:22:22.056139331 |
date '+%H:%M:%S.%N' | display nanoseconds |
YYYY-MM-DD hh:mm:ss
to Unix timestamp :1382618155
+%d/%m/%Y - %H:%M
'16/12/2013 - 18:39
UNIX time : 1234567890 ==> LOCAL TIME : Sat Feb 14 00:31:30 CET 2009 Central European Time UNIX time : 1500000000 ==> LOCAL TIME : Fri Jul 14 04:40:00 CEST 2017 UNIX time : 1600000000 ==> LOCAL TIME : Sun Sep 13 14:26:40 CEST 2020 Central European Summer Time, nothing's wrong with date UNIX time : 2000000000 ==> LOCAL TIME : Wed May 18 05:33:20 CEST 2033
startYear='2014' startMonth='08' startDay='06' startHour='13' startMinute='00' startSecond='00' nbDaysBack=4 resolutionInHours=1 # number of hours between 2 generated datetimes #dateTimeFormat='%d%m%y-%H' # ddmmyy-hh dateTimeFormat='%Y-%m-%d %H' # yyyy-mm-dd hh initialTimestamp=$(date -d"$startYear-$startMonth-$startDay $startHour:$startMinute:$startSecond" +%s) nbLoops=$((24/$resolutionInHours*$nbDaysBack)) timestamp=$initialTimestamp for i in $(seq 1 $nbLoops);do formattedDate=$(date -d@$timestamp +"$dateTimeFormat") echo $formattedDate # ... now do something smart with the formatted date ! timestamp=$((timestamp-(3600*$resolutionInHours))) done