Calculate 30/31 days from today date script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Calculate 30/31 days from today date script
# 1  
Old 07-09-2009
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..
# 2  
Old 07-09-2009
Please post the script in CODE tags
# 3  
Old 07-09-2009
#!/bin/ksh
is useless unless it it the first line in a script. See: https://www.unix.com/tips-tutorials/3...r-bin-ksh.html
# 4  
Old 07-09-2009
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 Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Compare Date to today's date in shell script

Hi Community! Following on from this code in another thread: #!/bin/bash file_string=`/bin/cat date.txt | /usr/bin/awk '{print $5,$4,$7,$6,$8}'` file_date=`/bin/date -d "$file_string"` file_epoch=`/bin/date -d "$file_string" +%s` now_epoch=`/bin/date +%s` if then #let... (2 Replies)
Discussion started by: Greenage
2 Replies

2. Linux

How to calculate the quarter end date according to the current date in shell script?

Hi, My question is how to calculate the quarter end date according to the current date in shell script? (2 Replies)
Discussion started by: Divya_1234
2 Replies

3. Shell Programming and Scripting

Get business days including today's date

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 (2 Replies)
Discussion started by: abhii
2 Replies

4. UNIX for Advanced & Expert Users

Script to rename file that was generated today and which starts with date

hello, can someone please suggest a script to rename a file that was generated today and filename that being generated daily starts with date, its a xml file. here is example. # find . -type f -mtime -1 ./20130529_4995733057260357019.xml # this finename should be renamed to this format.... (6 Replies)
Discussion started by: bobby320
6 Replies

5. Shell Programming and Scripting

UNIX date fuction - how to deduct days from today's date

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... (4 Replies)
Discussion started by: juzz4fun
4 Replies

6. Shell Programming and Scripting

Calculate the number of days between 2 dates - bash script

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! (3 Replies)
Discussion started by: kovacsakos
3 Replies

7. Shell Programming and Scripting

KSH script Not working (calculate days since 1/1/2000 given day 4444)

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... (5 Replies)
Discussion started by: thibodc
5 Replies

8. Shell Programming and Scripting

How to Get 60 days Old date from current date in KSH script

Hi i am writing a cron job. so for it i need the 60 days old date form current date in variable. Like today date is 27 jan 2011 then output value will be stote in variable in formet Nov 27. i am using EST date, and tried lot of solution and see lot of post but it did not helpful for me. so... (3 Replies)
Discussion started by: Himanshu_soni
3 Replies

9. Shell Programming and Scripting

Script to copy log files with today's date

I am a newbie to scripting. I need a korn shell script to copy log files of current day to archive folder and rename with current days date stamp. I would really appreciate your help. File structure is as follows. Everyday files get overwritten, so I need copy to a archive directory and... (3 Replies)
Discussion started by: mdncan
3 Replies

10. Shell Programming and Scripting

Script to calculate user's last login to check if > 90 days

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... (3 Replies)
Discussion started by: pdtak
3 Replies
Login or Register to Ask a Question