Getting yesterday DATE

Hi
It is possible pass to one program a parameter YESTERDAY DATE , i mean the current date less one day (sysdate -1) ?

Perderabo wrote a cool date script that will do what you ask and more! Click Here To Get the Script. Use the date_calc script to calculate the date you need then pass it as a parameter to your script. Good Luck.

I needed that functionality once too, I went about it by appending the current date to a file every day at 23:59 in cron... then refrencing that file whenever I needed to write a script to get things for yesterday... or last 5 days and so on and so forth.

yesterdaysdate=`head -1 $datefile`
past5days=`head -5 $datefile`

Interesting work-around; it sounds like you'd end up with a large file in a short amount of time though ... Perderabo's script is worth taking a look at. It has a ton of functionality to it.

Here is how I get yesterdays date

DATE_STAMP=`TZ=CST+24 date +%y%m%d`

3 Likes

Try the following command at your unix prompt:

perl -e 'print localtime(time() - 86400) . "\n" '

If your unix machine has Perl installed, then it will work.

This works but can you please tell for what we use "TZ" and "CST"???

The 'TZ' parameter permits overriding the default time zone. 'CST' is the name of a timezone.

Read the FAQ Yesterdays Date/Date Arithmetic

Jean-Pierre.

-------------------------------------------------------------
-------------------------------------------------------------

what this 86400 means

24 hours * 60 min * 60 sec = 86400 secs in a day.

So CST+24 and then the date command.....

so how does the time zone gets set.......

I have a Q. what is the difference between CST & PST?

TZ=CST+24 date %d
TZ=PST+24 date %d

if I want to get the "day" of a date, instead of using `datecalc -D {date}`, any other options?

perl -e '@arr=localtime(time() - 86400); print @arr[3]'

I do something similar. I put the date into a file, but I rename the files to age them. I.E.:

mv day29 day30
mv day28 day29
...
mv day01 day02
date +%D > day_01

One other thing I do is run it at around 11:00am Mon to Fri, and I first check to see if there are at least 'x' users logged in. 'x' is 70% of my typical number of users. This way my dates only contain business dates, and automatically skip holidays and snow days.

When using them, I always selected TWO day files. I.E. check to see if my date is <= day06 and > day07, or something similar.

This has bothered me for a while and I think I finally found a real solution in Solaris using the expr command to do the math for you.

you have to find today's date and put it in a variable.

today=`date +%Y%m%d`

Then run the expr command to get yesterday's date or two days ago, etc.

yesterday=`expr $today - 1`
echo $yesterday

This should work or does at least in Solaris 10.

Will not work in general. On January 1 your technique will report that yesterday was January 0.

The clever shell scripter is using an undocumented feature of the time zone command. It worked for +48 and -168 when I tested, but adding a semicolon to end the command ruins it. The CST is "Central Standard Time".

Yesterday's working day

RE: Shell script problem for you...

((D=$(date +%w)+2))
if [ $D -gt 3 ]; then D=1; fi
YESTERDAY=`TZ="GMT+$((D*24))" date  +%Y%m%d`; echo $YESTERDAY

I usually compute:

$date --date="yesterday"

works with tomorrow too.