get end of day epoch in shell script bash get end of day epoch in shell script bash shell shell

get end of day epoch in shell script bash


You can simply specify that with the -d argument.

i.e. date -d "today 23:59:59" +%swhere today 23:29:59 is used to get the end of the current day

Edit : @Toby propose the following approach to handle correctly leap seconds -d 'tomorrow 0 -1second

If you want the beginning of the day use

date -d "today 0" +%s


Using BSD date, you can use the -v to adjust the time properly.

% dateTue May 22 09:21:50 EDT 2018% date  -v 23H -v 59M -v 59STue May 22 23:59:59 EDT 2018

Slightly longer, but saving you from having to remember how many hours are in a day, minutes in an hour, etc, by going to midnight tomorrow, then subtracting one second.

% date -v +1d -v 0H -v 0M -v -0S -v -1S          ^      ^     ^      ^     ^          |      |     |      |     |          |      +-----+------+     subtract one second          |            |          |        reset to midnight          |          go to tomorrow

(It's a shame -v can't take a combination. date -v+1d0H0MS-1S would be nice to type. It's not terribly readable or obvious, but easy to parse if you know how -v works. Whitespace would make it better: date -v "+1d 0H 0M 0S -1S". #wishfulthinking)


A simple but fragile approach is to just round up to the nearest multiple of 86400:

$ now=$(date +%s)$ end_of_day=$(( now - now%86400 + 86399))

but this won't take daylight savings into account for the two days where it may be relevant.