tags:

views:

132

answers:

5

What is the easiest way to get the difference in months between two dates in C#?

ie: (date1 - date2).TotalMonths .. kind of thing. thanks!

+2  A: 

The best I can suggest is to get the total number of days, and then roughly compute the number of months by dividing accordingly. Something like:

DateTime dt1 = new DateTime( 2010, 10, 23);
DateTime dt2 = new DateTime( 2010, 7, 23);
TimeSpan ts = dt1 - dt2;
int days_per_month = 30;
Console.Write( ts.TotalDays / days_per_month);

If you really are okay with something like 2010 Feb 1 - 2010 Jan 31 returning 1 month as its answer, then given the above code, you would be able to get at this easily by using

Console.Write( dt1.Month - dt2.Month);

This doesn't take into consideration the year, so I defer to the other answer here that does this. :)

Dave
yeah thanks.. i have that now.. but not really accurate enough. I know with a larger method i can accurately calculate it.. just wondering if there is an easier way i am missing.
richardo van spusen
can you define "accuracy" when applied to a non-specific unit time like the month? :)
Dave
I just saw your comment to your original post. In this case, where Feb 1 - Jan 31 is one month, you just have to subtract the Month properties, i.e. dt1.Month - dt2.Month.
Dave
A: 

TimeSpan Class :)

 TimeSpan span = endTime.Subtract ( startTime );
 Console.WriteLine( "Time Difference (months): " + span.Days / 30 );
Chathuranga Chandrasekara
What about it? is that the easiest way?
richardo van spusen
It's inaccurate as months have varying number of days.
Will
@ Will Agreed :) Since he is using a "Large" unit, I just assumed he needs a rough value ;)
Chathuranga Chandrasekara
+1  A: 

If you don't know how to calculate date span in .net here is good example:

DateTime startTime = DateTime.Now;
DateTime endTime = DateTime.Now.AddSeconds(75);

TimeSpan span = endTime.Subtract ( startTime );
Console.WriteLine( "Time Difference (seconds): " + span.Seconds );
Console.WriteLine( "Time Difference (minutes): " + span.Minutes );
Console.WriteLine( "Time Difference (hours): " + span.Hours );
Console.WriteLine( "Time Difference (days): " + span.Days );

Source: here.

DateTime don't expose the difference in month since every month have a different number of days. The simplest way to get the month is totaldays / 30.

Mendy
Why so many up votes? This doesn't do what is required!
Rohan West
Have to agree... the question wasn't crystal clear, but this misses the mark by a wide margin, it's just a transparent copy-and-paste job from the very first Google hit for the exact text of the question. -1.
Aaronaught
+4  A: 

Since you already know that your dates will be the first of the month:

int totalMonths = (date2.Year - date1.Year)*12 + date2.Month - date1.Month;
Marcel Gosselin
+5  A: 

Given the updates you have made to your original question: How about writing up a function that takes two dates and does the following,

DateTime d1 = new DateTime(2008, 12, 1);
DateTime d2 = new DateTime(2009, 1, 1);

var month_diff = (d2.Year - d1.Year)*12 + (d2.Month - d1.Month);
Console.WriteLine(month_diff);
Gishu