08-04-2011 09:17 AM
I have two dates and I need difference in months (exact).
I found inck function, but it always rounds result on whole months. I need output as decimal number, for example 8.214 months. How to do this? Is there any function ot his purpose?
I can calculate difference in days and divide it by (30 ? 31), but the result would not be exact and i do not treat this as clean solution.
Thank you for an answer
08-04-2011 09:43 AM
that's the question, I would say that there would be some ISO standard about this, but i don't know.... do you?
Now I solve it that I divide number of days by (365.25/12).
08-04-2011 11:46 AM
I'm not aware of any standard way of doing this, and I'd be interested to understand why you want to see months in this way as I can't think off-hand of a reason for using this.
However, maybe something like the below will help. The variable month3 is a single-line version of the rest, which I left in to help make the calculation more clear. Presumably it could be tidied up a bit more...
format date1 date9.;
format date2 date9.;
date1 = '09FEB2011'd;
do i = -60 to 60 by 5;
date2 = intnx('day',date1,i);
months = intck('month',date1,date2);
temp = intnx('month',date1,months,'sameday');
days_shift = intck('day',temp,date2);
days_in_month = day(intnx('month',date2,0,'end'));
month2 = months + days_shift/days_in_month;
month3 = intck('month',date1,date2) + intck('day', intnx('month',date1,intck('month',date1,date2),'sameday'),date2) / day(intnx('month',date2,0,'end'));
To look at an example then (UK dates):
Using intck will say there is 1 month interval between the two, so Month = 1;
Temp is set to Date1 + Month, but to the same day, hence Temp = 09/03/2011.
There are -3 days between Temp and Date2, hence Days_Shift = -3.
There are 31 days in March, therefore Days_in_Month = 31.
Adding this together then says 1 full month, minus 3 days (which we take as the fraction 3 / 31), which is 28 / 31 or 0.9032 months.
The fraction element is determined by the number of days in the month of Date2, and it should work regardless as to which date is greater.
08-10-2011 06:25 PM
What is the exact number of months between 1-Jan and 31-Jan ?
What is an exact difference in months between two dates which fall on different days in the month?
Will it be fractional?
What should the fraction be when the startmonth has a different number of days from the finishing month?
sounds like an incomplete spec
best of luck
04-16-2014 06:41 PM
Bad specification. You should argue against it.
0.5 month in February is 14 days typically.
0.5 of December is 15.5 days. 1.5 days/30 days = 5% error introduced for no reason.
Use 365.25/12 = 30.4375 days as a month and calculate the difference as days and then divide by 30.4375 to get a slightly better definition.
04-16-2014 08:52 PM
please note that the question was asked about 3 years back and probably he might have fixed it and who knows.
Let's wait for his response to find what step he took. What do you say?
04-16-2014 03:08 PM
enddate1=intnx('month',date1,0,'end'); /*end date of date1 ? 30/31/28/29*/
format enddate1 enddate2 date9.; /*ddmmmyyyy*/
numdays1=day(enddate1); /*number of max days in that month*/
Months = (datdif (date1,enddate1, 'act/act')/numdays1 ) + intck('month',date1,date2) + (day(date2)/numdays2) -1; /* due to the limitation of intck function a -1 is adjusted*/
/*date1month+ date2month +and all months in between*/
5feb2014 - 17may2014
5feb-28feb/28 + 2+17may-1may/31