Hi Guys,
I was working some time ago n was in need to calculate date 30/31 days from today including Feb (Leap yr stuff). Today date is variable depending on day of execution of script. I tried searching but was not able to get exactly what I needed....So at that I time I implemented by my own which I have modified to generic so that it may helpful to you guys or somebody else like me in future...... Let me know if u find my problem with script
###############################################################################
# This Script add 30 days to current date and output next date including
# 28 or days days of February
# Author Vaibhav Gautam
##############################################################################
#!/bin/ksh
#Start validation date is today date and validation end date is +30 days
lastday=$(cal `date '+%m'` `date '+%Y'` | grep . | fmt -1 | tail -1 )
currentdate=$(date '+%d')
currentmonth=$(date '+%m')
year=$(date '+%Y')
temp=$(($lastday - $currentdate ))
echo "Date Calculation Started"
#Next date after 30 days
#Change Value 31 for 30 days
expirydate=$((30 - $temp ))
if [ $expirydate -gt 0 ]
then
nextmonth=$(($currentmonth + 1 ))
if [ $nextmonth -eq 2 ]
then
#If next month is feb then calculate last day of feb
lastday_nextmnth=$(cal 2 `date '+%Y'` | grep . | fmt -1 | tail -1 )
if [ $expirydate -gt $lastday_nextmnth ]
then
carrydate=$(($expirydate - $lastday_nextmnth))
nextmonth=$(($nextmonth + 1))
echo "Current Date $currentdate/$currentmonth/$year\" Next Date \"$carrydate/$nextmonth/$year\""
else
carrydate=$(($expirydate))
echo "Current Date $currentdate/$currentmonth/$year\" Next Date \"$carrydate/$currentmonth/$year\""
fi
else
if [ $nextmonth -gt 12 ]
then
nextmonth=1
nextyear=$(($year + 1 ))
echo "Current Date $currentdate/$currentmonth/$year\" Next Date \"$expirydate/$nextmonth/$nextyear\""
else
echo "Current Date $currentdate/$currentmonth/$year\" Next Date \"$expirydate/$nextmonth/$year\""
fi
fi
else
expirydate=$lastday
echo "Current Date $currentdate/$currentmonth/$year\" Next Date \"$expirydate/$currentmonth/$year\""
fi