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

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
# into $bad_date go here
# Suppose it is this:
bad_date='Jan 2 05:13:05'   # syslog formated date
# Convert to Epoch using GNU date
bad_epoch=$(date -d "$bad_date" '+%s')
# Apply correction
good_epoch=$(( bad_epoch + $CORRECTION ))
# Make corrected date human-readable
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 "bad_date:       $bad_date"
echo "bad_epoch:      $bad_epoch"
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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.