Today (Saturday) We will make some minor tuning adjustments to MySQL.

You may experience 2 up to 10 seconds "glitch time" when we restart MySQL. We expect to make these adjustments around 1AM Eastern Daylight Saving Time (EDT) US.


Calculate 30/31 days from today date script


Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
Calculate 30/31 days from today date script

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

Code:
###############################################################################
# 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


Last edited by coolgoose85; 07-09-2009 at 05:51 AM..
# 4  
If you are using ksh93, a much shorter solution is available using printf.
Code:
$ date
Thu Jul  9 09:48:10 EDT 2009
$ printf "%T\n" "now + 30 days"
Sat Aug  8 09:48:47 EDT 2009
$

Login or Register to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Get business days including today's date
abhii
I am trying to get last 5 business day . trying for d in Mon Tue Wed Thu Fri do date +%Y%m%d -d "last $d" done gives me date Thu Oct 20 23:56:26 EDT 2016 20161017 20161018 20161019 20161013 20161014 expected output should be 20161017 20161018 20161019 20161020... Shell Programming and Scripting
2
Shell Programming and Scripting
UNIX date fuction - how to deduct days from today's date
juzz4fun
Hi, One of my Unix scripts needs to look for files coming in on Fridays. This script runs on Mondays. $date +"%y%m%d" will give me today's date. How can I get previous Friday's date.. can I do "today's date minus 3 days" to get Friday's date? If not, then any other way?? Name of the files is...... Shell Programming and Scripting
4
Shell Programming and Scripting
Calculate the number of days between 2 dates - bash script
kovacsakos
I wrote the day calculator also in bash. I would like to now, that is it good so? #!/bin/bash datum1=`date -d "1991/1/1" "+%s"` datum2=`date "+%s"` diff=$(($datum2-$datum1)) days=$(($diff/(60*60*24))) echo $days Thanks in advance for your help!... Shell Programming and Scripting
3
Shell Programming and Scripting
KSH script Not working (calculate days since 1/1/2000 given day 4444)
thibodc
I am unable to get this KSH script to work. Can someone help. I've been told this should work with KSH93. Which I think I have on Solaris 10. If I do a grep -i version /usr/dt/bin/dtksh I get @(#)Version M-12/28/93d @(#)Version 12/28/93 @(#)Version M-12/28/93 This is correct for...... Shell Programming and Scripting
5
Shell Programming and Scripting
Script to calculate user's last login to check if > 90 days
pdtak
I need a script to figure out if a user's last login was 90 days or older. OS=AIX 5.3, shell=Korn Here's what I have so far: ==== #!/usr/bin/ksh NOW=`lsuser -a time_last_login root | awk -F= '{ print $2 }'` (( LAST_LOGIN_TIME = 0 )) (( DIFF = $NOW - $LAST_LOGIN_TIME )) lsuser -a...... Shell Programming and Scripting
3
Shell Programming and Scripting

Featured Tech Videos