man date
won't show me any reserved words but they have 'now' or 'today' etc.
I wonder if there is any clear way to print start(00:00:00) of the day.
Will date -u -d "$(date +'%F')"
suffice in any situation? Looks a little dumb.
date -d 00:00
or date -d 00:00:00
work with the GNU, ast-open, toybox and busybox implementations of date
.
date -d 0
also works with GNU date
.
$ date -d 00:00
Tue 4 Oct 00:00:00 BST 2022
With BSD date
:
$ date -jf %H%M%S 000000
Mon Oct 4 00:00:00 BST 2022
None of -j
, -f
, -d
are standard option to date
.
POSIX date
has no provision for specifying an input date other than now except for setting the system clock (under the XSI option).
You could do:
$ date '+%a %e 00:00:00 %Z %Y'
Tue 4 00:00:00 BST 2022
But the BST
part can end up being wrong if you run that on the day the clock changes from/to winter time.
ksh93
and zsh
have builtin support for parsing input dates:
ksh93
$ printf '%(%c [%s epoch time])T\n' 00:00
Tue Oct 4 00:00:00 2022 [1664838000 epoch time]
(bash
copied the %(format)T
from ksh93 but not the ability to specify input dates other than in epoch seconds).
ksh93 can also be built with the ast-open date
builtin.
zsh
$ zmodload zsh/datetime
$ strftime -s today %F
$ strftime -rs start %F $today
$ strftime '%c [%s epoch time]' $start
Tue 04 Oct 2022 00:00:00 BST [1664838000 epoch time]
Or getting today's date using prompt expansion (here via the %
parameter expansion flag):
$ zmodload zsh/datetime
$ strftime -rs start %F ${(%):-%D{%F}}
$ strftime '%c [%s epoch time]' $start
Tue 04 Oct 2022 00:00:00 BST [1664838000 epoch time]
Note that your:
date -u -d "$(date +'%F')"
Takes today's date in your timezone in YYYY-MM-DD format, but then asks date
to give you the time for 00:00:00 UTC on that day, which unless you're in a timezone that currently happens to align with UTC will not be the start of your day.
Here in mainland Britain which is still in summer time (one hour ahead of UTC):
$ date -u -d "$(date +'%F')" +%s
1664841600
$ date -d 00:00 +%s
1664838000
TZ=Europe/London faketime 2022-10-30T00:01 date -d tomorrow +%F
returning 2022-10-30 with current versions of GNUdate
. – Stéphane Chazelas Oct 05 '22 at 09:12date
has put some thought into those DST issues, so it may work as intended and documented, I'd have to look again in the doc and source to check. – Stéphane Chazelas Oct 05 '22 at 09:37info date 'Relative items in date strings'
.tomorrow
is the same as1 day
orday
and aday
is 24 hours of 3600 seconds, not a calendar day. – Stéphane Chazelas Oct 05 '22 at 09:41TZ=Europe/London faketime 2022-10-30T00:01 date -d 'tomorrow 00:00:00' +%FT%T%z
returns2022-10-31T00:00:00+0000
. – Stéphane Chazelas Oct 05 '22 at 09:44