I have the following in a shell script. How can I subtract one hour while retaining the formatting?
DATE=`date "+%m/%d/%Y -%H:%M:%S"`
I have the following in a shell script. How can I subtract one hour while retaining the formatting?
DATE=`date "+%m/%d/%Y -%H:%M:%S"`
Here another way to subtract 1 hour.
I hope that It can help someone. Best Regards!
Convert to timestamp (a long integer), subtract the right number of milliseconds, reformat to the format you need.
Hard to give more details since you don't specify a programming language...
if you need substract with timestamp :
If you have bash version
4.4+
you can use bash's internal date printing and arithmetics:The
$(printf "%(%s)T")
prints the epoch seconds, the$(( epoch - 60*60 ))
is bash-aritmetics - subtracting 1hour in seconds. Prints:The following command works on recent versions of GNU
date
:This work on my Ubuntu 16.04
date
:date --date="@$(($(date +%s) - 3600))" "+%m/%d/%Y -%H:%M:%S"
And thedate
version isdate (GNU coreutils) 8.25