Number of months between two dates

Post questions here relative to DataStage Enterprise/PX Edition for such areas as Parallel job design, Parallel datasets, BuildOps, Wrappers, etc.

Moderators: chulett, rschirm, roy

Post Reply
reddyamarnath
Participant
Posts: 8
Joined: Thu Jun 17, 2010 1:12 am

Number of months between two dates

Post by reddyamarnath »

Hi
can any one please help me how to calculate the number of months between two dates.

Thanks
reddy,amarnath
Etler21
Participant
Posts: 52
Joined: Mon Mar 08, 2010 5:51 am

Post by Etler21 »

you can use MonthfromDate() function to get month out and then subtract.
qt_ky
Premium Member
Premium Member
Posts: 2895
Joined: Wed Aug 03, 2011 6:16 am
Location: USA

Post by qt_ky »

That may depend on your requirements and definition on month: 28, 29, 30, or 31 days? How many months would you say are between Jan. 31 and Feb. 1? How many between Jan. 31 and Mar. 1? etc.
Choose a job you love, and you will never have to work a day in your life. - Confucius
jwiles
Premium Member
Premium Member
Posts: 1274
Joined: Sun Nov 14, 2004 8:50 pm
Contact:

Post by jwiles »

One method that worked for a client several years ago was:

Code: Select all

(Year1 - Year2) * 12 + (Month1 - Month2) + (if Day1 >= Day2 then 1 else 0)
where Date1 is the later of the two dates (Year1, Month1, Day1). That particular client didn't care about the number of days in the month for the calculation. How you extract Year, Month and Day will depend on the format of your dates.

Regards,
- james wiles


All generalizations are false, including this one - Mark Twain.
Post Reply