January 17, 2007
How to get yesterdays (or tomorrows) date in Unix

Ive just found this neat trick on google for getting a date in Unix other than todays without any fiddling, perl scripts, complicated maths, or anything else. Just run

TZ=GMT+24 date ‘+%d/%m/%y’

and you will get yesterdays date

TZ=GMT-24 date ‘+%d/%m/%y’

will get you tomorrows date.

This works in Korn Shell on AIX, C Shell on Solaris, Bash on Linux, and I imagine almost everything else out there.

You can do any multiple of 24 to get you a date, e.g. -144 will give you the date in 7 days time, it handles month changes, year changes, lead years, everything.

Bookmark and Share
Bookmark and Share