I want total months between two date (I/p)

i want exactly four batch on the basis of date , for ex.

i/p Start date : 01-JUN-2005 (or format can be 20050601 - YYYYMMDD)
i/p End Date : 01-OCT-2006 or format can be 20061001 - YYYYMMDD)

so in this scenario each batch should be 4 months (as there are 16 months in between this two dates)

so if anyone having function that return me the total number of months between two dates so that i can divide it and add in to my start date

for ex. total months / 4 i.e. 16/4

01-JUN-2005 + 4 = 01-OCT-2005
01-OCT-2005 + 4 = 01-FEB-2006
01-FEB-2005 + 4 = 01-JUN-2006
01-JUN-2006 + 4 = 01-OCT-2006

even if its possible then please provide function that having addition of month to date

3
Contributors
5
Replies
6
Views
7 Years
Discussion Span
Last Post by kevinkatia

You can convert the date to epoch seconds do the math and convert back. Here is a sample from one of my reference books:

``````CORRECTION='172800'   # 2 days worth of seconds
# Code to extract the date portion from the data
# Suppose it is this:
bad_date='Jan 2 05:13:05'   # syslog formated date
# Convert to Epoch using GNU date
# Apply correction
good_date=\$(date -d "1970-01-01 UTC \$good_epoch seconds")    # GNU Date
good_date_iso=\$(date -d "1970-01-01 UTC \$good_epoch seconds" +'%Y-%m-%d %T') # GNU
Date
echo "Correction:     +\$CORRECTION"
echo "good_epoch:     \$good_epoch"
echo "good_date:      \$good_date"
echo "good_date_iso:  \$good_date_iso"
# Code to insert the \$good_date back into the data goes here``````

Thanks rch1231 for quick and valuable reply.

I want total months between two date (I/p)

i want exactly four batch on the basis of date , for ex.

i/p Start date : 01-JUN-2005 (or format can be 20050601 - YYYYMMDD)
i/p End Date : 01-OCT-2006 or format can be 20061001 - YYYYMMDD)

so in this scenario each batch should be 4 months (as there are 16 months in between this two dates)

so if anyone having function that return me the total number of months between two dates so that i can divide it and add in to my start date

for ex. total months / 4 i.e. 16/4

01-JUN-2005 + 4 = 01-OCT-2005
01-OCT-2005 + 4 = 01-FEB-2006
01-FEB-2005 + 4 = 01-JUN-2006
01-JUN-2006 + 4 = 01-OCT-2006

even if its possible then please provide function that having addition of month to date

So do we consider this solved?

yes , actually i implemented same on java as my environment gave me flexibility to intgrate with Java.

Thanks for this script

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.