Return yesterday's date
- From: "Stu" <beefstu350@xxxxxxxxxxx>
- Date: 1 Jun 2006 13:11:55 -0700
Here is a generic function. Just manipulate the days_old variable. I
have it set so it figures out what yesterday is.
Enjoy.
#!/bin/ksh
axsp_dateSub()
{
# ARG_1 -> The date to subtract days DDMMYYYY format
# ARG_2 -> The number of days to subtract
# Check that the input is valid.
# There should be exactly 2 arguments.
if [ $# -ne 2 ]; then
return 1
fi
typeset -i n
n=`expr $2 + 0`
day=""
month=""
year=""
typeset -i day=`echo $1 | cut -c1-2`
typeset -i month=`echo $1 | cut -c3-4`
year=`echo $1 | cut -c5-8`
# Add 0 to month. This is a
# trick to make month an unpadded integer.
month=`expr $month + 0`
# Subtract n from the current day.
day=`expr $day - $n`
# While the day is less than or equal to
# 0, deincrement the month.
while [ $day -le 0 ]
do
month=`expr $month - 1`
# If month is 0 then it is Dec of last year.
if [ $month -eq 0 ]; then
year=`expr $year - 1`
month=12
fi
# Add the number of days appropriate to the
# month.
case $month in
1|3|5|7|8|10|12) day=`expr $day + 31`;;
4|6|9|11) day=`expr $day + 30`;;
2)
if [ `expr $year % 4` -eq 0 ]; then
if [ `expr $year % 400` -eq 0 ]; then
day=`expr $day + 29`
elif [ `expr $year % 100` -eq 0 ]; then
day=`expr $day + 28`
else
day=`expr $day + 29`
fi
else
day=`expr $day + 28`
fi
;;
esac
done
# Pad the integers before sending
day_str=$day
[ $day -lt 10 ] && day_str="0$day"
mon_str=$month
[ $month -lt 10 ] && mon_str="0$month"
echo ${year}${mon_str}${day_str}
exit 0
}
current_date=`date +%d%m%Y`
days_old=1
YYYYMMDD=$(axsp_dateSub "$current_date" "${days_old}" )
Chuck wrote:
Is there a simple way in a ksh script to get the previous day's date in
YYYYMMDD format? I was hoping to find something that will do it in one
or two lines of code.
.
- Follow-Ups:
- Re: Return yesterday's date
- From: Chris F.A. Johnson
- Re: Return yesterday's date
- Prev by Date: Re: a cron problem
- Next by Date: Re: sed help
- Previous by thread: Re: Return yesterday's date
- Next by thread: Re: Return yesterday's date
- Index(es):