Showing results for 
Search instead for 
Do you mean 

extract previous date

Highlighted
Occasional Advisor

extract previous date

Hi

I need to extract the previous day's date in the form of mmddyyyy in my script.

Can anyone please help me.

Thanks,
Pat


8 REPLIES
Honored Contributor Honored Contributor

Re: extract previous date

Clay Stephenson of these forums has a very nifty utility for this purpose both in shell script and perl formats.

please cruise to this page and scroll down to the bottom. You will see the links to download either version.

http://www.hpux.ws/merijn/

Hope this helps

________________________________
UNIX because I majored in cryptology...
Acclaimed Contributor Acclaimed Contributor

Re: extract previous date

I'm sure that you will see some responses that tell you to alter TZ and then call date but that only works for limited ranges of TZ's and in any event works by accident. This solution , using the attached caljd.sh, script will work anywhere and for any number of days past or future:

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

Invoke as caljd.sh -u for full usage and examples.
If it ain't broke, I can fix that.
Acclaimed Contributor Acclaimed Contributor

Re: extract previous date

I'll also throw in a Perl solution leveraging the localtime() function and subtracting 1 day (86400 seconds) from current time.

YESTERDAY=$(perl -e '($mday,$mon,$year) = (localtime(time() - 86400)) [3,4,5]; printf("%02d%02d%04d\n",$mon + 1,$mday,$year + 1900)')
echo "Yesterday was ${YESTERDAY}"

If I got all the ()'s,[]'s, and quotes right that ought to work but I think the caljd.sh solution is much more readable.
If it ain't broke, I can fix that.
Trusted Contributor Trusted Contributor

Re: extract previous date

I got this off of the web from a while ago. This is meant for gathering the last month, but you can substitute for day as well. You can gather pretty much what you need from this.

LAST_MONTH=$((`date +%m` -1))
case "$LAST_MONTH"
in
0) LM=Dec;;
1) LM=Jan;;
2) LM=Feb;;
3) LM=Mar;;
4) LM=Apr;;
5) LM=May;;
6) LM=Jun;;
7) LM=Jul;;
8) LM=Aug;;
9) LM=Sep;;
10) LM=Oct;;
11) LM=Nov;;
12) LM=Dec;;

esac
Advisor

Re: extract previous date

More than one way to do it, but here's my quick-and-dirty perl:

perl -MPOSIX -e 'print POSIX::strftime("%Y%m%d", localtime(time - 86400));'

Ed
Advisor

Re: extract previous date

Guess that should be perl -MPOSIX -e 'print POSIX::strftime("%m%d%Y", localtime(time - 86400));'
Occasional Advisor

Re: extract previous date

Hi,

Thanks for all the help. I have used Clay's solution which works absolutely fine.

Clay - Can you please suggest me how to use your script in case I want yesterday's date to be in the format mmddyy (020605).

Thanks,
Pat

Acclaimed Contributor Acclaimed Contributor

Re: extract previous date

Here's the Perl method for 2 digit years:

YESTERDAY=$(perl -e '($mday,$mon,$year) = (localtime(time() - 86400)) [3,4,5]; printf("%02d%02d%02d\n",$mon + 1,$mday,$year % 100)')
echo "Yesterday was ${YESTERDAY}"

... and here's the caljd.sh method:

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

If you had invoked caljd.sh as caljd.sh -u, you would have seen the "-C" option and been able to answer your own question.
If it ain't broke, I can fix that.