tags:

views:

8637

answers:

8

How to get difference between two dates in Year/Month/Week/Day in an efficient way?

eg. difference between two dates is 1 Year, 2 Months, 3 Weeks, 4 Days.

Difference represents count of year(s), month(s), week(s) and day(s) between two dates.

+4  A: 

If you subtract two instances of DateTime, that will return an instance of TimeSpan, which will represent the difference between the two dates.

Andy
The largest unit of time represented by TimeSpan is days. However, since both years (leap year) and months vary in length, you'd have to do some math to figure them out anyway, and they're non-deterministic because the actual year values matter but you wouldn't have that information in the TimeSpan structure itself.
GalacticCowboy
A: 

Use the Subtract method of the DateTime object which returns a TimeSpan...

DateTime dt1 = new DateTime(2009, 3, 14);
DateTime dt2 = new DateTime(2008, 3, 15);

TimeSpan ts = dt1.Subtract(dt2);

Double days = ts.TotalDays;
Double hours = ts.TotalHours;
Double years = ts.TotalDays / 365;
Chalkey
+3  A: 

Subtract two DateTime instances to give you a TimeSpan which has a Days property. (E.g. in PowerShell):

PS > ([datetime]::today - [datetime]"2009-04-07")


Days              : 89
Hours             : 0
Minutes           : 0
Seconds           : 0
Milliseconds      : 0
Ticks             : 76896000000000
TotalDays         : 89
TotalHours        : 2136
TotalMinutes      : 128160
TotalSeconds      : 7689600
TotalMilliseconds : 7689600000

Converting days into years or weeks is relatively easy (days in a year could be 365, 365.25, ... depending on context). Months is much harder, because without a base date you don't know which month lengths apply.

Assuming you want to start with your base date, you can incrementally substract while counting first years (checking for leap years), then month lengths (indexing from startDate.Month), then weeks (remaining days divided by 7) and then days (remainder).

There are a lot of edge cases to consider, e.g. 2005-03-01 is one year from 2004-03-01, and from 2004-02-29 depending on what you mean by "Year".

Richard
Year: count of years between two dates.
Ahmed
Yes, you need to deal with the years and months first, before you know how many days are left for you to calculate weeks and days.
GalacticCowboy
@Ahmed: OK apply to the three dates I list in the last paragraph. 2004-02-29 plus one year is what date? 2004-03-1 plus one year is what date?
Richard
+5  A: 

Leap years and uneven months actually make this a non-trivial problem. I'm sure someone can come up with a more efficient way, but here's one option - approximate on the small side first and adjust up (untested):

public static void GetDifference(DateTime date1, DateTime date2, out int Years, 
    out int Months, out int Weeks, out int Days)
{
    //assumes date2 is the bigger date for simplicity

    //years
    TimeSpan diff = date2 - date1;
    Years = diff.Days / 366;
    DateTime workingDate = date1.AddYears(Years);

    while(workingDate.AddYears(1) <= date2)
    {
        workingDate = workingDate.AddYears(1);
        Years++;
    }

    //months
    diff = date2 - workingDate;
    Months = diff.Days / 31;
    workingDate = workingDate.AddMonths(Months);

    while(workingDate.AddMonths(1) <= date2)
    {
        workingDate = workingDate.AddMonths(1);
        Months++;
    }

    //weeks and days
    diff = date2 - workingDate;
    Weeks = diff.Days / 7; //weeks always have 7 days
    Days = diff.Days % 7;
}
lc
I suspect DateTime.AddMonths could cause problems here due to the uneven months. (Jan 31st + 1 month?)
Jon Skeet
@Jon Skeet - Yes, that's a very good point. How to handle it, however, will depend on your definition of a "difference of a month" too...any suggestions?
lc
+1 - I think this solution gives what is probably the most intuitive result (as does the AddMonths method which gives Feb 28th or 29th as one month after January 31st). We wouldn't hesitate to say that Feb 1st is one month after Jan 1st and, at the same time, say March 1st is one month after Feb 1st... even though there are 2 or three days difference in the months' lengths.
Traples
One more thing... I might change it so that it compares using "<=" rather than just "<". This way, 7/5/09 is "1 month" more than 6/5/09 instead of "4 weeks and 2 days."
Traples
@Traples Agreed.
lc
+7  A: 

This is actually quite tricky. A different total number of days can result in the same result. For example:

  • 19th June 2008 to 19th June 2010 = 2 years, but also 365 * 2 days

  • 19th June 2006 to 19th June 2008 = 2 years, but also 365 + 366 days due to leap years

You may well want to subtract years until you get to the point where you've got two dates which are less than a year apart. Then subtract months until you get to the point where you've got two dates which are less than a month apart.

Further confusion: subtracting (or adding) months is tricky when you might start with a date of "30th March" - what's a month earlier than that?

Even further confusion (may not be relevant): even a day isn't always 24 hours. Daylight saving anyone?

Even further confusion (almost certainly not relevant): even a minute isn't always 60 seconds. Leap seconds are highly confusing...

I don't have the time to work out the exact right way of doing this right now - this answer is mostly to raise the fact that it's not nearly as simple as it might sound.

EDIT: Unfortunately I'm not going to have enough time to answer this fully. I would suggest you start off by defining a struct representing a Period:

public struct Period
{
    private readonly int days;
    public int Days { get { return days; } }
    private readonly int months;
    public int Months { get { return months; } }
    private readonly int years;
    public int Years { get { return years; } }

    public Period(int years, int months, int days)
    {
        this.years = years;
        this.months = months;
        this.days = days;
    }

    public Period WithDays(int newDays)
    {
        return new Period(years, months, newDays);
    }

    public Period WithMonths(int newMonths)
    {
        return new Period(years, newMonths, days);
    }

    public Period WithYears(int newYears)
    {
        return new Period(newYears, months, days);
    }

    public static DateTime operator +(DateTime date, Period period)
    {
        // TODO: Implement this!
    }

    public static Period Difference(DateTime first, DateTime second)
    {
        // TODO: Implement this!
    }
}

I suggest you implement the + operator first, which should inform the Difference method - you should make sure that first + (Period.Difference(first, second)) == second for all first/second values.

Start with writing a whole slew of unit tests - initially "easy" cases, then move on to tricky ones involving leap years. I know the normal approach is to write one test at a time, but I'd personally brainstorm a bunch of them before you start any implementation work.

Allow yourself a day to implement this properly. It's tricky stuff.

Note that I've omitted weeks here - that value at least is easy, because it's always 7 days. So given a (positive) period, you'd have:

int years = period.Years;
int months = period.Months;
int weeks = period.Days / 7;
int daysWithinWeek = period.Days % 7;

(I suggest you avoid even thinking about negative periods - make sure everything is positive, all the time.)

Jon Skeet
I eager to see your 'exact right way of doing this'.
Ahmed
@Ahmed: I'll see if I can find time later.
Jon Skeet
A: 

DateTime dt1 = new DateTime(2009, 3, 14); DateTime dt2 = new DateTime(2008, 3, 15);

int diffMonth = Math.Abs((dt2.Year - dt1.Year)*12 + dt1.Month - dt2.Month)

+2  A: 

What about using the System.Data.Linq namespace and its SqlMethods.DateDiffMonth method?

For example, say...

DateTime starDT = {01-Jul-2009 12:00:00 AM}

DateTime endDT = {01-Nov-2009 12:00:00 AM}

then

int monthDiff = System.Data.Linq.SqlClient.SqlMethods.DateDiffMonth(startDT, endDT);

==> 4

There are other DateDiff static methods in the SqlMethods class.

sheir
A: 

Hi, I have written a comprehensive program to give the difference between two dates in C. Please find the original work here.

http://www.technicalypto.com/2010/02/different-between-two-days-implemented.html

Bragboy
Aisde from this being in C when the question was clearly about C#, this is not even remotely the same thing. If all he wanted was the total number of days in between then he could have just done `(date2 - date1).TotalDays`.
Aaronaught
I apologize.. I kind of posted this hastily. Will be more careful in future answers.
Bragboy