Get yesterday’s date in bash on Linux, DST-safe

I have a shell script that runs on Linux and uses this call to get yesterday’s date in YYYY-MM-DD format:

date -d "1 day ago" '+%Y-%m-%d'

It works most of the time, but when the script ran yesterday morning at 2013-03-11 0:35 CDT it returned "2013-03-09" instead of "2013-03-10".

Presumably daylight saving time (which started yesterday) is to blame. I’m guessing the way "1 day ago" is implemented it subtracted 24 hours, and 24 hours before 2013-03-11 0:35 CDT was 2013-03-09 23:35 CST, which led to the result of "2013-03-09".

So what’s a good DST-safe way to get yesterday’s date in bash on Linux?

10 Answers
10

Leave a Comment