Operating System - HP-UX
1819704 Members
3446 Online
109605 Solutions
New Discussion юеВ

Re: Can I get yesterday's date in a unix script?

 
SOLVED
Go to solution
Dick Fischer
Occasional Advisor

Can I get yesterday's date in a unix script?

When running a unix script I am familiar with getting a system date variable in the format yyyymmdd, by using the following command: variable_name=`date +%Y%m%d`.

Is there a relatively painless way of getting yesterday's date into a variable? (i.e. variable_name2=??????????)

Thanks.
30 REPLIES 30
G. Vrijhoeven
Honored Contributor

Re: Can I get yesterday's date in a unix script?

Hi Dick,

file=`TZ=$TZ+24 date "+%y%m%d"`

thanks Chris..
Check this link.

http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=222442HTH,

Gideon
A. Clay Stephenson
Acclaimed Contributor

Re: Can I get yesterday's date in a unix script?

While you can play games with the TZ setting and then invoke date, that only works for TZ's with small offsets and always puts you in the situation where TZ is set to an out of bounds condition.

The most foolproof way is to use Perl's date command subtracting 86400 from the current time or simply use this script which can do much more than just 1 day forward or backwards.

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

Invoke as caljd.sh -u for full usage and examples.

If it ain't broke, I can fix that.
H.Merijn Brand (procura
Honored Contributor

Re: Can I get yesterday's date in a unix script?

A day of FAQ's it is.

From the FAQ section on my site:

--8<---
How do I print Yesterday?

d3:/ 102 > echo 'Yesterday'
Yesterday
d3:/ 103 > perl -le 'print scalar localtime time - 86400'

Or use A.Clay Stephenson's date hammer, which can also be used for more complicated actions from the command line, as will using the modules Date::Calc or Date::Manip with Perl.
-->8---

Clay's date hammer will be mentioned a lot when you search for this question on the forum search pages. It is also available from my site

fiddling with timezone's helps (sometimes)

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
H.Merijn Brand (procura
Honored Contributor

Re: Can I get yesterday's date in a unix script?

A day of FAQ's it is.

From the FAQ section on my site:

--8<---
How do I print Yesterday?

d3:/ 102 > echo 'Yesterday'
Yesterday
d3:/ 103 > perl -le 'print scalar localtime time - 86400'

Or use A.Clay Stephenson's date hammer, which can also be used for more complicated actions from the command line, as will using the modules Date::Calc or Date::Manip with Perl.
-->8---

Clay's date hammer will be mentioned a lot when you search for this question on the forum search pages. It is also available from my site

fiddling with timezone's helps (sometimes)

My HP ITRC site pages can be found at

Singapore https://www.beepz.com/personal/merijn/
Rotterdam http://www.cmve.net/~merijn/
Seattle http://www.hpux.ws/merijn/

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Dick Fischer
Occasional Advisor

Re: Can I get yesterday's date in a unix script?

Thank you all for your suggestions. Gideon,
since you were the first to reply I gave your example the first shot.

Here's my code:
today=`date +%Y%m%d`
yesterday=`TZ=$TZ+24 date "+%Y%m%d"`
echo "today is " $today
echo "yesterday is " $yesterday

When I execute it, here are my results:
today is 20040419
yesterday is 20040419

I also tried it with "-24" and by taking the "+24" out altogether, all with the same results.

(I tried taking a look at the link you provided, but it's down at the moment.)
A. Clay Stephenson
Acclaimed Contributor

Re: Can I get yesterday's date in a unix script?

Those tricks of playing with TZ to force the offset past a 24 hour boundary only work by accident and are very not portable. Man 5 environ and look at the specifications relating to TZ.

Use a more robust techique, Perl, caljd.sh or the Gnu date command.

If it ain't broke, I can fix that.
Dave La Mar
Honored Contributor
Solution

Re: Can I get yesterday's date in a unix script?

Dick -
If you insist on using TZ -
TZ=PST8PDT+24 date +%Y%m%d | read Yesterday

Where PST8PDT is the offset which may be different in your locale, simply substitue with your offset.

Best regards,

dl
"I'm not dumb. I just have a command of thoroughly useless information."
Dick Fischer
Occasional Advisor

Re: Can I get yesterday's date in a unix script?

Thank you all for your responses. Dave, it looks like I'll be going with your solution (although I believe you meant PST*PDT, not PST8PDT).

It's not that I insist on using TZ. I tried downloading A. Clay's caljd.sh, but couldn't get it to work.

Since I'm using this basically for display purposes anyway, it's not a big deal if it hiccups once in a while.

(I'm closing this discussion.)
John Wolfe_1
Advisor

Re: Can I get yesterday's date in a unix script?

Hi Dick:

I know you said that this was closed but I just tried the version of caljd.sh that Clay attached and it worked just as expected. The only trouble that I have ever had with his script was when I tried to run it on Solaris. I just changed #!/usr/bin/sh to #!/usr/bin/ksh to use the korn shell and it worked perfectly. You should really learn how to use this script. It will solve almost any date problem you can think of.

Regards,
John
At least I have a job.
Qi Jian Gang
Occasional Advisor

Re: Can I get yesterday's date in a unix script?

try attached simple c program.
pizza
Santosh_16
Visitor

Re: Can I get yesterday's date in a unix script?

Hi Dick,

Hope this script will help

#!/bin/ksh
### Script to get Yesterday Date #############

Y=`date +'%Y'`
M=`date +'%m'`
D=`date +'%d'`
today=$Y$M$D
echo $today
yesterday=$Y$M`expr $D - 1`
echo $yesterday

Regards,
Santosh
rmueller58
Valued Contributor

Re: Can I get yesterday's date in a unix script?

Santosh's script works for me.. simple and to the point.. I've been using one similar to it to rotate logs off my system.

H.Merijn Brand (procura
Honored Contributor

Re: Can I get yesterday's date in a unix script?

Not that I think that Santosh should not post, but the fact that Rex advertises his practises as good, makes me put some question marks here.

This is the most unreliable piece of date calculation I've ever seen here on the forum.

Ever considered what happens on every first day of each month?

No month rolovers, no year rollovers, no check for valid dates.

Please do not you it in production work, unless you actually don't care that "yesterday" might e invalid

Enjoy, Have FUN! H.Merijn [ who advises to get caljd running ! ]
Enjoy, Have FUN! H.Merijn
Santosh_16
Visitor

Re: Can I get yesterday's date in a unix script?

Hi Procura,

Thanks for Pointing out that. I am sorry if you have something better than this please share it with the fourm.

Hi Dick,

This New Script takes care of Month Rollover , hope this will be useful to you now.

Cheers
Santosh


#!/bin/ksh
### Script to get Yesterday Date #############

Y=`date +'%Y'`
M=`date +'%m'`
D=`date +'%d'`
today=$Y$M$D
echo $today

if [ $M -eq 01 -o $M -eq 03 -o $M -eq 05 -o $M -eq 07 -o $M -eq 08 -o $M -eq 10 -o $M -eq 12 ] && [ $D -eq 01 ];then
yesterday=$Y$M`expr $D + 30`
echo $yesterday
else
if [ $M -eq 04 -o $M -eq 06 -o $M -eq 09 -o $M -eq 11 ] && [ $D -eq 01 ]; then
yesterday=$Y$M`expr $D + 29`
echo $yesterday
else
if [ $M -eq 02 -a $D -eq 01 ]; then
yesterday=$Y$M`expr $D + 27`
echo $yesterday
else
yesterday=$Y$M`expr $D - 1`
echo $yesterday
fi
fi
fi
H.Merijn Brand (procura
Honored Contributor

Re: Can I get yesterday's date in a unix script?

Yes, I have something better, and I already shared it here:

lt09:/home/merijn 103 > perl -le 'print scalar localtime time - 86400'
Tue Apr 20 12:16:45 2004
lt09:/home/merijn 104 > perl -e'@x=localtime time-86400;printf"%4d%02d%02d\n",$x[5]+1900,$x[4]+1,$x[3]'
20040420
lt09:/home/merijn 105 >

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
A. Clay Stephenson
Acclaimed Contributor

Re: Can I get yesterday's date in a unix script?

Sorry Santosh,

You still have a bug. Your code doesn't handle leap years --- and remember century years are not leap years unless they are divisible by 400.

Date calculations are always a pain.
If it ain't broke, I can fix that.
sara stricker
New Member

Re: Can I get yesterday's date in a unix script?

Hello Dick,

I read your question and thought you may be able to pass the advice on the date question I have listed below. I tried to calculate n22edate which go back 375/675 days from the date I entered but get the wrong date calculation

read sdate?"Enter first day of report period (YYYYMMDD) "
read rdate?"Enter last day of report period (YYYYMMDD) "

export n22edate1=`expr $sdate - 375`
export n22sdate1=`expr $rdate - 675`

echo n22edate1 $n22edate1
echo n22sdate1 $n22sdate1

export n22edate=`echo "$n22edate1" | cut -c 1-6`"31"
export n22sdate=`echo "$n22sdate1" | cut -c 1-6`"01"

echo n22edate $n22edate
echo n22sdate $n22sdate
**********OUTPUT*********'
Enter first day of report period (YYYYMMDD) 20040810
Enter last day of report period (YYYYMMDD) 20050531
mo 05
yr 05
n22edate1 20040435
n22sdate1 20049856
n22edate 20040431
n22sdate 20049801

It's most appreciated if you are willing to advice.

Thanks,
Sara
A. Clay Stephenson
Acclaimed Contributor

Re: Can I get yesterday's date in a unix script?

Sara, you should probably start your own thread but this should get you started:

#!/usr/bin/sh

OFFSET1=375
echo "Enter 1st date of report period (YYYY MM DD): \c"
read YR MO DAY
DT1=$(caljd.sh -y $(caljd.sh -y -p ${OFFSET1} ${YR} ${MO} ${DAY}))
echo "${OFFSET1} days before was ${DT1}"

Make sure the attached script, caljd.sh is somewhere in your path and make it executable.

Invoke as caljd.sh -u for usage and many examples.
If it ain't broke, I can fix that.
Babu Yalamanchi
Advisor

Re: Can I get yesterday's date in a unix script?

perl -e'@x=localtime time-86400;printf"%4d%02d%02d\n",$x[5]+1900,$x[4]+1,$x[3]'

Hi

I need to extend this script to be able to pass a parameter of 1, or 2 or 3 days(i.e. yesterday, the day before etc). Can the author of this script or some body else
help?
Thanks

P.S. I tried to use the TZ script and caljd.sh. TZ worked only for yesterday. Caljd.sh didn't work at all.
Patrick Wallek
Honored Contributor

Re: Can I get yesterday's date in a unix script?

What problem did you have with caljd.sh? That is a VERY reliable script. Most issues with that are just not knowing precisely how to use it.

caljd.sh -u

will give full usage instructions.
A. Clay Stephenson
Acclaimed Contributor

Re: Can I get yesterday's date in a unix script?

Caljd.sh works perfectly for me:

Pasted from telnet session:
[prissy]/home/cstephen: P3=$(caljd.sh -y -S "/" $(caljd.pl -p 3))
[prissy]/home/cstephen: echo "${P3}"
2006/08/29
[prissy]/home/cstephen:

It displays 3 days before now (1-Sep).

Vrsn 2.27 is the latest version and it is attached just above your last posting. I suspect, what you are not telling me is that you are running a non POSIX/non Korn shell (bash?). If you are on Linux, run it under zsh.


In any event, the Perl script just needs to change 86400 to some integer multiple (negative or positive) to adjust the time to days before or days after the current date.
If it ain't broke, I can fix that.
Babu Yalamanchi
Advisor

Re: Can I get yesterday's date in a unix script?

A.C, Patrick

caljd.sh is a large shell that needs to be saved some place and called from my script. My shop is very small and there's nobody to maintain it if I am not here. However, it is extremely flexible. When I tried to copy and paste in a 'vi' session, I had problem with the size of vi buffer. However, I saved and copied the file and made it work. The perl script can be incorporated in my script. I only need to pass a parameter that will have only 1, 2 or 3. I don't know how to pass a parameter to perl. Hence the post. You both converted me to use caljd.sh

Thanks a lot. Problem solved.

A. Clay Stephenson
Acclaimed Contributor

Re: Can I get yesterday's date in a unix script?

Although I could easily modify the Perl one-liner to do the task you describe, it does you no favor to reward laziness. Learn Perl or Plan B: Use your shell scripting skills to replace the 86400 in the perl one-liner with a multiple of 86400 and then call Perl. Essentially, you would be building the Perl snippet "on the fly" and that should be well within your grasp.
If it ain't broke, I can fix that.
jmckinzie
Super Advisor

Re: Can I get yesterday's date in a unix script?

This is simple...
simply type this:

DTE=$(date +%Y%m%d -1)
((Y=DTE - 1))
print $Y
20060902
print $DTE
20060903

Done...its as simple as that!