Operating System - HP-UX
1833780 Members
2315 Online
110063 Solutions
New Discussion

calculate the date of yesterday

 
Kelvin Yu
Advisor

calculate the date of yesterday

Hi,

I read some old postings and found the following, but need clarification:

Case #1
-------
export TZ=
date

Case #2
-------
Yesterday=`TZ=aaa24 date +%Y%m%d`

If I put this in a shell script that runs by crontab, which is preferable?

Thanks,
Kelvin
2 REPLIES 2
Geoff Wild
Honored Contributor

Re: calculate the date of yesterday

I like case 2 - straight to the point - less processing...etc.

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
A. Clay Stephenson
Acclaimed Contributor

Re: calculate the date of yesterday

The problem with this approach is that it is not portable and does not work with all TZ settings. The behavior of the date functions is undefined when the offset is out of range. A much better approach is to convert the current date to a Julian Day and then subtract 1 from it and then redisplay the Julian Day as a calendar date.

YESTERDAY=$(caljd.sh -y -s $(caljd.sh -p 1))
echo "Yesterday = ${YESTERDAY}"

This will output in your desired format.


The caljd.sh will work for any number of days in the past by changing -p n (or days in the future by changing -p n to -n n). Invoke as caljd.sh -u for full usage.

If it ain't broke, I can fix that.