.net - Calculate number of fractional month between two dates (C#) -
i number of (potentially fractional) calendar months between 2 days e.g. 2 jan 2013 - 15 feb 2014 should around 12.5 months.
i surprised not find answered on google.
edit: ended writing code - here's answer if needs same (my karma day :)
/// <summary> /// number of total calendar months between 2 dates. if day of month different, /// gives fractional approximation using average days per month. /// </summary> public static double monthsbetween(datetime start, datetime finish) { //handle if dates switched - calculation same there's negative result: double multiplier; if(finish < start) { var temp = start; start = finish; finish = temp; multiplier = -1; } else { multiplier = 1; } //1) 20 mar 2012 - 13 jan 2014 --> 2*12 months //2) 15 jan 2011 - 30 jul 2012 --> 1*12 months //3) 20 jan 2010 - 25 jan 2010 --> 0*12 months double totalmonths = (finish.year - start.year)*12; //1) 20 mar 2012 - 13 jan 2014 --> 2*12 + 1 - 3 = 22 months //2) 15 jan 2011 - 30 jul 2012 --> 1*12 + 7 - 1 = 18 months //3) 20 jan 2010 - 25 jan 2010 --> 0*12 + 0 months = 0 months totalmonths += finish.month - start.month; ///now have "1st of month 1st of month" difference. days can approximated, ///since each month has different number of days. statistically (http://en.wikipedia.org/wiki/month#julian_and_gregorian_calendars): const double averagedaysinmonth = 30.436875; ///remove days we've included in starting month (not in actual period): totalmonths -= start.day / averagedaysinmonth; ///add days in finish month (weren't yet included, since had "1st 1st"): totalmonths += finish.day / averagedaysinmonth; //1) 20 mar 2012 - 13 jan 2014 --> 2*12 + 1 - 3 - 20/30 + 13/30 = 22 - 7/30 = 21.76 months //2) 15 jan 2011 - 30 jul 2012 --> 1*12 + 7 - 1 - 15/30 + 30/30 = 18 + 15/30 = 18.5 months //3) 20 jan 2010 - 25 jan 2010 --> 0*12 + 0 - 20/30 + 25/30 = 0 + 5/30 = 0.17 months return totalmonths * multiplier; }
likewise, realised after need similar years. here's code too, in case helps somebody:
/// <summary> /// number of total calendar years between 2 dates. gives fractional /// approximation if months/days differ. /// </summary> public static double yearsbetween(datetime start, datetime finish) { //handle if dates switched - calculation same there's negative result: double multiplier; if (finish < start) { var temp = start; start = finish; finish = temp; multiplier = -1; } else { multiplier = 1; } //1) 20 mar 2012 - 13 jan 2014 --> 2 years //2) 15 jan 2011 - 30 jul 2012 --> 1 year //3) 20 jan 2010 - 25 jan 2010 --> 0 years double totalyears = finish.year - start.year; ///now have "1st of year 1st of year" difference. days/months can approximated, ///since each year has different number of days. statistically (http://en.wikipedia.org/wiki/year): const double averagedaysperyear = 365.2425; ///remove days we've included in starting year (not in actual period): totalyears -= start.dayofyear / averagedaysperyear; ///add days in finish year (weren't yet included, since had "jan 1 jan 1"): totalyears += finish.dayofyear / averagedaysperyear; //1) 20 mar 2012 - 13 jan 2014 --> 2 - ~(2*30+20)/365 + 13/365 = 1.82 years //2) 15 jan 2011 - 30 jul 2012 --> 1 - 15/365 + ~(6*30+30)/365 = 1.53 years //3) 20 jan 2010 - 25 jan 2010 --> 0 - 20/365 + 25/365 = 0.01 years return totalyears * multiplier; }
use following:
timespan timespan = laterdate.subtract(earlierdate); var monthscount = timespan.totaldays / monthdayscount;
Comments
Post a Comment