Simpler Navigation for Servers and Operating Systems
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
cancel
Showing results for 
Search instead for 
Did you mean: 

date manipulating

SOLVED
Go to solution
Matt Dunfee
Occasional Contributor

date manipulating

Does anyone know a simple way to script the previous days' date? More specifically, "current day - 1".
9 REPLIES
linuxfan
Honored Contributor

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
harry d brown jr
Honored Contributor

Re: date manipulating

Highlighted
Santosh Nair_1
Honored Contributor

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
A. Clay Stephenson
Acclaimed Contributor

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.
Matt Dunfee
Occasional Contributor

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!!
A. Clay Stephenson
Acclaimed Contributor
Solution

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.
James R. Ferguson
Acclaimed Contributor

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...
linuxfan
Honored Contributor

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
linuxfan
Honored Contributor

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