Operating System - HP-UX
1832978 Members
2775 Online
110048 Solutions
New Discussion

Re: How do I subtract 1 day from date

 
Helen Herring
Frequent Advisor

How do I subtract 1 day from date

I have a script with:

Day = 'date +%a'

dumpfile="holdfile_${Day}.dmp.Z

I need to subract 1 day from the date.
Can you

10 REPLIES 10
linuxfan
Honored Contributor

Re: How do I subtract 1 day from date

Hi Kathy,

You could do something as simple as

YESTERDAY=`TZ=PST+24 date +%D`
or in perl
yesterday=$(perl -e 'print scalar localtime(time-1*86400),"\n"' | cut -c 5-11)

If you want more, just do a search in the forums, you will get quite a few options

-Regards
Ramesh
They think they know but don't. At least I know I don't know - Socrates
linuxfan
Honored Contributor

Re: How do I subtract 1 day from date

Hi Kathy,

My mistake, you are using
Day = `date +%a`

which gives the day of the week, so you may have to use
yest=`TZ=PST+24 date +%a`

-HTH
Ramesh
They think they know but don't. At least I know I don't know - Socrates
Joseph C. Denman
Honored Contributor

Re: How do I subtract 1 day from date

For something like keeping track of dumps and this like that, I have found it easier to user julian date.

DAY=`date +%j`
YESTERDAY=`expr $DAY - 1`

...jcd...
If I had only read the instructions first??
Helen Herring
Frequent Advisor

Re: How do I subtract 1 day from date

I want to use the day of the week. Can the -1 be used with day if day has been set to the day of the week?
Joseph C. Denman
Honored Contributor

Re: How do I subtract 1 day from date

Sure, the only problem will be Monday which is 1. However, you can fix that:

DAY=`date +%u` #DAYS of week mon-sun 1-7
if [ $DAY -eq 1 ]
then
YESTERDAY=7
else
YESTERDAY=`expr $DAY - 1`
fi

...jcd...
If I had only read the instructions first??
Jeffrey S. Sims
Trusted Contributor

Re: How do I subtract 1 day from date

If you have perl running on your system try using this command:

perl -e 'print scalar localtime(time-1*86400),"\n"'

And yes this works with text dates
Sridhar Bhaskarla
Honored Contributor

Re: How do I subtract 1 day from date

Kathy,

I will prepare a table like this


MON=1
TUES=2
WED=3
THU=4
FRI=5
SAT=6
SUN=7

NOW you can use the same trick as told by Joseph.

DAY=`date +%u`
if [ $DAY -eq 1 ]
then
YESTERDAY=7
else
YESTERDAY=`expr $DAY - 1`
YESTERDAY_IS=`grep YESTERDAY table |awk '{FS=
"=";print $1}`
fi

You can use YESTERDAY_IS in your program to replace the day of the week.

-Sri


You may be disappointed if you fail, but you are doomed if you don't try
Sridhar Bhaskarla
Honored Contributor

Re: How do I subtract 1 day from date

Kathy,

I will prepare a table like this


MON=1
TUES=2
WED=3
THU=4
FRI=5
SAT=6
SUN=7

NOW you can use the same trick as told by Joseph.

DAY=`date +%u`
if [ $DAY -eq 1 ]
then
YESTERDAY=7
else
YESTERDAY=`expr $DAY - 1`
YESTERDAY_IS=`grep $YESTERDAY table |awk '{FS=
"=";print $1}`
fi

You can use YESTERDAY_IS in your program to replace the day of the week.

-Sri


You may be disappointed if you fail, but you are doomed if you don't try
Paula J Frazer-Campbell
Honored Contributor

Re: How do I subtract 1 day from date

Hi

Here is a nice script written by Tom Danzig that will work back to 01/01/01.

usage is :- datwhen -n

HTH

Paula
If you can spell SysAdmin then you is one - anon
Paula J Frazer-Campbell
Honored Contributor

Re: How do I subtract 1 day from date

ps

to clarify the datewhen bit :- that is what I called the script on my server.

Paula
If you can spell SysAdmin then you is one - anon