Calculate the month difference between two dates in C#


There are a lot of written methos of getting the difference between two dates, but I couldn't find the one I needed. I need to have a real difference like 3.75 months between the two dates.

Not every month has the same amount of days and the February is also an issue here. So I made myself a method doing this:

/// <summary>
                      /// Get months difference between two dates
                      /// </summary>
                      /// <param name="from">from date</param>
                      /// <param name="to">to date</param>
                      /// <returns>months (double) between </returns>
                      internal static double GetMonths(DateTime from, DateTime to)
                      	/// |-------X----|---------------|---------------|--X-----------|
                      	///         ^                                       ^
                      	///       from                                     to
                      	//change the dates if to is before from
                      	if (to.Ticks < from.Ticks)
                      		DateTime temp = from;
                      		from = to;
                      		to = temp;
                      	/// Gets the day percentage of the months = 0...1
                      	/// 0            1                               0              1
                      	/// |-------X----|---------------|---------------|--X-----------|
                      	/// ^^^^^^^^^                                    ^^^^
                      	/// percFrom                                    percTo
                      	double percFrom = (double)from.Day / DateTime.DaysInMonth(from.Year, from.Month);
                      	double percTo = (double)to.Day / DateTime.DaysInMonth(to.Year, to.Month);
                      	/// get the amount of months between the two dates based on day one
                      	/// |-------X----|---------------|---------------|--X-----------|
                      	/// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                      	///                        months
                      	double months = (to.Year * 12 + to.Month) - (from.Year * 12 + from.Month);
                      	/// Return the right parts
                      	/// |-------X----|---------------|---------------|--X-----------|            
                      	///         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                      	///                      return
                      	return months - percFrom + percTo;            

Open in new window

So what this method does is seeing every month as a number between the 0 and the 1.
It counts the months between the two dates using day=1 and correct this number with the percentages calculated. Using the buit in method for getting the DaysInMonth, you're fine with every month, even February.


Comments (3)


Hi mahmuod,

I know, but using this you get the real difference between two dates, like the days between the two dates (Double). In the TimeSpan, there is no Month property.

And in my case, the method is using one month as 100%, so every month can have different amount of days. So the same amount of days between two dates can have multiple results depending on the choosen dates.
because of the complexity of the question, have you mapped out the sum on paper?  

if you can get the sum right using proper math, you should then be able to recreate the sum in a method \ function and return the value accordingly.

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.