Re: extract previous date (180 Views)
Reply
Occasional Advisor
Pat Peter
Posts: 23
Registered: ‎02-04-2005
Message 1 of 9 (180 Views)

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


Please use plain text.
Honored Contributor
Mel Burslan
Posts: 3,212
Registered: ‎08-26-1998
Message 2 of 9 (180 Views)

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...
Please use plain text.
Acclaimed Contributor
A. Clay Stephenson
Posts: 17,825
Registered: ‎07-16-1998
Message 3 of 9 (180 Views)

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.
Please use plain text.
Acclaimed Contributor
A. Clay Stephenson
Posts: 17,825
Registered: ‎07-16-1998
Message 4 of 9 (180 Views)

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.
Please use plain text.
Trusted Contributor
Sheriff Andy
Posts: 258
Registered: ‎03-21-2004
Message 5 of 9 (180 Views)

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
Please use plain text.
Advisor
Ed Loehr
Posts: 31
Registered: ‎08-18-2004
Message 6 of 9 (180 Views)

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
Please use plain text.
Advisor
Ed Loehr
Posts: 31
Registered: ‎08-18-2004
Message 7 of 9 (180 Views)

Re: extract previous date

Guess that should be perl -MPOSIX -e 'print POSIX::strftime("%m%d%Y", localtime(time - 86400));'
Please use plain text.
Occasional Advisor
Pat Peter
Posts: 23
Registered: ‎02-04-2005
Message 8 of 9 (180 Views)

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

Please use plain text.
Acclaimed Contributor
A. Clay Stephenson
Posts: 17,825
Registered: ‎07-16-1998
Message 9 of 9 (180 Views)

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.
Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation