06-06-2014 02:18 PM
I need to calculate number of months and weeks between two dates.
Eg. start date ' 01Jan2014' and end date is '14Feb2014' so the difference is 1.5 months.
Any help is appreciated.
Thanks in advance
06-06-2014 02:45 PM
Well, it all depends on what is in your data now. Based on your post, I'm going to assume that you have two character strings, for example:
start_date = '01jan2014';
end_date = '14feb2014';
If your variables actually contain something different, the formula will be different. Now you can compute:
n_days = input(end_date,date9.) - input(start_date,date9.);
That will give you the difference in days, and you can convert that to weeks or months in any way that pleases you.
06-06-2014 03:22 PM
That's sort of a difficult question because what is a month isn't clear from month to month. From your example, the duration would actually be 1.4465 months or 1.4 months
Depending on what you're doing its better to pick either a different interval or some way to standardize it.
I would for example, calculate weeks as days_diff/7.
@ksharp had a good solution for months difference that uses the yrdif function.
start_date = '01jan2014'd;
end_date = '14feb2014'd;
months=yrdif(start_date, end_date, 'act/act')*12;
06-06-2014 04:13 PM
months=intck('month',date1,date2) + ((intck('days',date1,date2)/31)-1);
06-06-2014 04:49 PM
Getting an intuitively correct decimal for the number of months is quite tricky (and likely useless) but here goes:
startDate = '01Jan2014'd;
endDate = '14Feb2014'd;
sb = intnx("MONTH",startDate,0);
se = intnx("MONTH",startDate,0,"E");
eb = intnx("MONTH",endDate,0);
ee = intnx("MONTH",endDate,0,"E");
m = intck("MONTH",startDate,endDate);
months = round(m - 1 + 2*(se-startDate+endDate-eb+2)/(se-sb+ee-eb+2), 0.1);
weeks = round(intck("DAY", startDate, endDate) / 7, 0.1);
put months= weeks=;
06-07-2014 05:40 PM
check this link.
you will want to use this syntax when you need to be precise.