Date - incorrect results for previous date

Hello:
I am bit puzzled with what I could be doing wrong and any help is appreciated.
I have a date in YYYMMDD format and I need to find the previous date. Based on the input on this forum, I have come up with the following. It seems to work for all except the following. Here I am passing date "20140310" and my expected output is "20140309" but I am getting "20140308". Bit weird....

$ date -d 20140310 +%s
1394424000
# now deduct 86400 sec for 24 hours
$ expr 1394424000 - 86400
1394337600
$ date +"%Y%m%d" -d @1394337600
20140308

I suspect time zones are involved. TZ="GMT" date ...

1 Like

It's probably related to daylight savings adjustments.

Try specifying Timezone to UTC for each data command:

$ TZ=UTC date -d 20140310 +%s
1394409600
$ expr 1394409600  - 86400
1394323200
$ TZ=UTC date +"%Y%m%d" -d @1394323200
20140309
1 Like

Hmm. If I am in a EST with daylight savings observed, what needs to be changed to the above. I use this logic for a set of dates and 20140310 is just n example

---------- Post updated at 04:32 PM ---------- Previous update was at 04:28 PM ----------

Thanks carnoa and chuber, that fixes