Re: date manipulating (298 Views)
Reply
Occasional Contributor
Matt Dunfee
Posts: 9
Registered: ‎01-25-2001
Message 1 of 10 (298 Views)
Accepted Solution

date manipulating

Does anyone know a simple way to script the previous days' date? More specifically, "current day - 1".
Honored Contributor
linuxfan
Posts: 874
Registered: ‎10-19-2000
Message 2 of 10 (298 Views)

Re: date manipulating

Hi Matt,


There a number of ways you can do it

1. YD=`TZ=aaa24 date +%Y%m%d`
this will store yesterdays date in YD in the format YYMMDD

2.Yesterday=`TZ=PST+24 date +%D`
this will give it in the format 10/08/01

3. export YD=$(perl -e 'print scalar localtime(time-1*86400),"\n"' |cut -c 5-11)

this will just store Oct 8 in YD variable.


-HTH
Ramesh
They think they know but don't. At least I know I don't know - Socrates
Honored Contributor
harry d brown jr
Posts: 8,418
Registered: ‎12-12-2000
Message 3 of 10 (298 Views)

Re: date manipulating

Honored Contributor
Santosh Nair_1
Posts: 1,020
Registered: ‎07-18-2000
Message 4 of 10 (298 Views)

Re: date manipulating

Try playing around with the TIMEZONE environment variable.

echo $(sh -c "TZ=$(date +%Z)+24; export TZ; date '+%m%d%y'")

See this URL for more info:

http://www.dutchworks.nl/htbin/hpsysadmin?h=3&dn=19828&q=date%20script&fh

-Santosh
Life is what's happening while you're busy making other plans
Acclaimed Contributor
A. Clay Stephenson
Posts: 17,825
Registered: ‎07-16-1998
Message 5 of 10 (298 Views)

Re: date manipulating

Hi:

I'll give you my standard answer. I always use Julian days to do this - astromers use this method so that leap years, months, ... don't make calculations messy. If you call the attached script with no args, it converts today's date into a Julian Day (~ number of days since 4004 BCE); subtract 1 from this and feed that number in and it returns a calendar date. This method will work across any number of days past or future.

e.g.

#!/usr/bin/sh
TODAYJDATE=`caljd.sh`
PREVJDATE=$(( ${TODAYJDATE} - 1))
PREVDATE=`caljd.sh ${PREVJDATE}`
echo "Yesterday was ${PREVDATE}"

Clay
If it ain't broke, I can fix that.
Occasional Contributor
Matt Dunfee
Posts: 9
Registered: ‎01-25-2001
Message 6 of 10 (298 Views)

Re: date manipulating

Ramesh - Using
Yesterday=`TZ=CST+24 date +%D` works perfect! The only stipulation is, how could I filter for weekends to display Friday's date on Monday?

Thanks!!
Acclaimed Contributor
A. Clay Stephenson
Posts: 17,825
Registered: ‎07-16-1998
Message 7 of 10 (298 Views)

Re: date manipulating

Now Matt, we are getting to the beauty of true Julian Dates: When supplied with -w caljd.sh returns the weekday (0 - Sun; 6 -Sat.)

So:

#!/usr/bin/sh
TODAYJDATE=`caljd.sh`
TODAYWKDAY=`caljd.sh -w`
NDAYS=1
#if Monday subtract 3 else subtract 1
if [ ${TODAYWKDAY} -eq 1 ]
then
NDAYS=3
fi
PREVJDATE=$(( ${TODAYJDATE} - ${NDAYS} ))
PREVDATE=`caljd.sh ${PREVJDATE}`
echo "Yesterday was ${PREVDATE}"

Clay
If it ain't broke, I can fix that.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 8 of 10 (298 Views)

Re: date manipulating

Hi Matt:

The trick posted by Ramesh only works +-24 hours, so that you can get tomorrorw's or yesterday's date but nothing beyond those bounds.

Regards!

...JRF...
Honored Contributor
linuxfan
Posts: 874
Registered: ‎10-19-2000
Message 9 of 10 (298 Views)

Re: date manipulating

Hi Matt,

As pointed out by James and as you have already found out, my suggestion only works for +/- 24 hours, the best way to achieve what you want is to use the caljd.sh script provided by clay.

You could do something like this

/Begin/
#!/usr/bin/ksh

# THis script calculates the previous date of weekdays
# Uses caljd.sh

PATH=/usr/bin:/usr/local/bin:
# You do want to modify the path to include the caljd.sh script

TODAYJDATE=`caljd.sh`
TODAY_WK_DAY=`caljd.sh -w`

if [ $TODAY_WK_DAY -ge 2 -a $TODAY_WK_DAY -le 5 ]
then
{
PREVJDATE=$(( ${TODAYJDATE} - 1))
PREVDATE=`caljd.sh ${PREVJDATE}`
echo "Yesterday was ${PREVDATE}"
}
elif [ $TODAY_WK_DAY -eq 1 ]
then
{
PREVJDATE=$(( ${TODAYJDATE} - 3))
PREVDATE=`caljd.sh ${PREVJDATE}`
echo "Yesterday was ${PREVDATE}"
}
elif [ $TODAY_WK_DAY -eq 0 ]
then
{
PREVJDATE=$(( ${TODAYJDATE} - 2))
PREVDATE=`caljd.sh ${PREVJDATE}`
echo "Yesterday was ${PREVDATE}"
}
elif [ $TODAY_WK_DAY = 6 ]
then
{
PREVJDATE=$(( ${TODAYJDATE} - 1))
PREVDATE=`caljd.sh ${PREVJDATE}`
echo "Yesterday was ${PREVDATE}"
}
fi

/End/

This would work for all the scenarious

-HTH
Ramesh
They think they know but don't. At least I know I don't know - Socrates
Honored Contributor
linuxfan
Posts: 874
Registered: ‎10-19-2000
Message 10 of 10 (298 Views)

Re: date manipulating

Hi again,


Modified version,


/Begin/

#!/usr/bin/ksh

# THis script calculates the date depending on weekdays
# Uses caljd.sh

PATH=/usr/bin:/usr/local/bin:~/admin/scripts

TODAYJDATE=`caljd.sh`
# TODAY_WK_DAY=`caljd.sh -w`
TODAY_WK_DAY=6

if [ $TODAY_WK_DAY -ge 2 -a $TODAY_WK_DAY -le 6 ]
then
{
PREVJDATE=$(( ${TODAYJDATE} - 1))
PREVDATE=`caljd.sh ${PREVJDATE}`
echo "Yesterday was ${PREVDATE}"
}
elif [ $TODAY_WK_DAY -eq 1 ]
then
{
PREVJDATE=$(( ${TODAYJDATE} - 3))
PREVDATE=`caljd.sh ${PREVJDATE}`
echo "Yesterday was ${PREVDATE}"
}
elif [ $TODAY_WK_DAY -eq 0 ]
then
{
PREVJDATE=$(( ${TODAYJDATE} - 2))
PREVDATE=`caljd.sh ${PREVJDATE}`
echo "Yesterday was ${PREVDATE}"
}
fi


/End/

-Regards
Ramesh
They think they know but don't. At least I know I don't know - Socrates
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.