Re: Script with problem for getting dates (120 Views)
Reply
Frequent Advisor
Rita Li
Posts: 59
Registered: ‎09-17-2000
Message 1 of 4 (120 Views)
Accepted Solution

Script with problem for getting dates

Hi,

Today is Oct-1-2007, a Monday date. My script runs werid that is to start with getting the last Sunday date, then the Monday date before this Sunday date.

day1=`date "+%Y%m%d"`
day2=`date +%w`
if [ $day2 -eq 0 ]; then
sun_day=$day1
else
sun_day=`date +%Y%m%d-$day2|bc`
fi
mon_day=`date +%Y%m%d-$day2-6|bc`
echo $sun_day $mon_day

The result produced is:

20071000 20070994

Pls render help

Rita
Honored Contributor
Steven Schweda
Posts: 9,088
Registered: ‎02-23-2005
Message 2 of 4 (120 Views)

Re: Script with problem for getting dates

What's weird? 20071001 - 1 = 20071000, and
20071001 - 1 - 6 = 20070994. You got what
you asked for.

The result of "date '+%Y%m%d'" is not an
ordinary decimal number, so you can't do
ordinary decimal arithmetic on it and get
a resonable answer, except by luck.

You might look at the suggestions for a
similar problem at:

http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=1040167
Honored Contributor
TY 007
Posts: 377
Registered: ‎08-06-2007
Message 3 of 4 (120 Views)

Re: Script with problem for getting dates

Hello Rita,

date '+%m %d %Y' |
{
read MONTH DAY YEAR
DAY=`expr "$DAY" - 1`
case "$DAY" in
0)
MONTH=`expr "$MONTH" - 1`
case "$MONTH" in
0)
MONTH=12
YEAR=`expr "$YEAR" - 1`
;;
esac
DAY=`cal $MONTH $YEAR | grep . | fmt -1 | tail -1`
esac
sun_day=$YEAR/$MONTH/$DAY
mon_day=`date +%Y/%m/%d`
echo $sun_day $mon_day
}

The result produced is:

2007/9/30 2007/10/01

Please modify above script further.

Thanks

Frequent Advisor
Rita Li
Posts: 59
Registered: ‎09-17-2000
Message 4 of 4 (120 Views)

Re: Script with problem for getting dates

A. Clay's script is great

Problem is fixed

Thank you for the prompt respond

Rita
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.