<

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x

Calculate the month difference between two dates in C#

Published on
20,491 Points
14,191 Views
3 Endorsements
Last Modified:
Approved
Hi,

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.

Regards,
Erik
3
Comment
Author:TGB01
3 Comments

Expert Comment

by:mahmuod
0
LVL 3

Author Comment

by:TGB01
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.
0
LVL 1

Expert Comment

by:SimonPrice3376
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.
0

Featured Post

Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

Join & Write a Comment

A query can call a function, and a function can call Excel, even though we are in Access. This is Part 2, and steps you through the VBA that "wraps" Excel functionality so we can use its worksheet functions in Access. The declaration statement de…
Discover the basics of using Outlook 2016 from office 365.
Next Article:

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month