user6223604
user6223604

Reputation:

Script shell how to decrease x minutes to a given date?

I need to decrease 10 minutes to a given date :

givenDate = 2017-11-07 19:20:37

when executing :

newDate=$(date +'%Y-%m-%d %T' --date="$givenDate - 10 minutes")
echo $newDate

I get :

2017-11-08 06:21:37

instead of

2017-11-07 19:10:37

Please Help. Thanks you.

Upvotes: 2

Views: 112

Answers (2)

Ipor Sircer
Ipor Sircer

Reputation: 3141

date is waiting for date in format "Sun, 29 Feb 2004 16:21:42 -0800", so your "- 10 minutes" appending is treated as timezone + 1 minute.

So, add your timezone to the reference date:

$ givenDate="2017-11-07 19:20:37 $(date +%:::z)"

Upvotes: 3

OznOg
OznOg

Reputation: 4722

try :

 date +'%Y-%m-%d %T' --date="-10 minutes $givendate"

Upvotes: 4

Related Questions