Note that there are some explanatory texts on larger screens.

plurals
  1. POCalculate number of fractional month between two dates (C#)
    primarykey
    data
    text
    <p>I would like the number of (potentially fractional) calendar months between two days e.g. 2 Jan 2013 - 15 Feb 2014 should be around 12.5 months.</p> <p>I was surprised not to find this already answered on Google.</p> <p>EDIT: I ended up writing some code - here's my answer if anybody needs the same (my Good Karma for the day :)</p> <pre><code>/// &lt;summary&gt; /// Number of total calendar months between two dates. If day of month is different, /// gives fractional approximation using average days per month. /// &lt;/summary&gt; public static double MonthsBetween(DateTime start, DateTime finish) { //handle if dates switched - calculation same but there's a negative result: double multiplier; if(finish &lt; start) { var temp = start; start = finish; finish = temp; multiplier = -1; } else { multiplier = 1; } //1) 20 Mar 2012 - 13 Jan 2014 --&gt; 2*12 months //2) 15 Jan 2011 - 30 Jul 2012 --&gt; 1*12 months //3) 20 Jan 2010 - 25 Jan 2010 --&gt; 0*12 months double totalMonths = (finish.Year - start.Year)*12; //1) 20 Mar 2012 - 13 Jan 2014 --&gt; 2*12 + 1 - 3 = 22 months //2) 15 Jan 2011 - 30 Jul 2012 --&gt; 1*12 + 7 - 1 = 18 months //3) 20 Jan 2010 - 25 Jan 2010 --&gt; 0*12 + 0 months = 0 months totalMonths += finish.Month - start.Month; ///Now we have "1st of the month to 1st of the month" difference. Days can only be approximated, ///since each month has a different number of days. Statistically (http://en.wikipedia.org/wiki/Month#Julian_and_Gregorian_calendars): const double averageDaysInMonth = 30.436875; ///Remove the days we've included in the starting month (not in actual period): totalMonths -= start.Day / averageDaysInMonth; ///Add the days in the finish month (weren't yet included, since had "1st to 1st"): totalMonths += finish.Day / averageDaysInMonth; //1) 20 Mar 2012 - 13 Jan 2014 --&gt; 2*12 + 1 - 3 - 20/30 + 13/30 = 22 - 7/30 = 21.76 months //2) 15 Jan 2011 - 30 Jul 2012 --&gt; 1*12 + 7 - 1 - 15/30 + 30/30 = 18 + 15/30 = 18.5 months //3) 20 Jan 2010 - 25 Jan 2010 --&gt; 0*12 + 0 - 20/30 + 25/30 = 0 + 5/30 = 0.17 months return totalMonths * multiplier; } </code></pre> <p>Likewise, I realised just after I need something similar for years. Here's that code too, in case it helps somebody:</p> <pre><code>/// &lt;summary&gt; /// Number of total calendar years between two dates. Gives fractional /// approximation if months/days differ. /// &lt;/summary&gt; public static double YearsBetween(DateTime start, DateTime finish) { //handle if dates switched - calculation same but there's a negative result: double multiplier; if (finish &lt; start) { var temp = start; start = finish; finish = temp; multiplier = -1; } else { multiplier = 1; } //1) 20 Mar 2012 - 13 Jan 2014 --&gt; 2 years //2) 15 Jan 2011 - 30 Jul 2012 --&gt; 1 year //3) 20 Jan 2010 - 25 Jan 2010 --&gt; 0 years double totalYears = finish.Year - start.Year; ///Now we have "1st of the year to 1st of the year" difference. Days/months can only be approximated, ///since each year has a different number of days. Statistically (http://en.wikipedia.org/wiki/Year): const double averageDaysPerYear = 365.2425; ///Remove the days we've included in the starting year (not in actual period): totalYears -= start.DayOfYear / averageDaysPerYear; ///Add the days in the finish year (weren't yet included, since had "Jan 1 to Jan 1"): totalYears += finish.DayOfYear / averageDaysPerYear; //1) 20 Mar 2012 - 13 Jan 2014 --&gt; 2 - ~(2*30+20)/365 + 13/365 = 1.82 years //2) 15 Jan 2011 - 30 Jul 2012 --&gt; 1 - 15/365 + ~(6*30+30)/365 = 1.53 years //3) 20 Jan 2010 - 25 Jan 2010 --&gt; 0 - 20/365 + 25/365 = 0.01 years return totalYears * multiplier; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload