StartingFromScratch
StartingFromScratch

Reputation: 628

DateTime difference in days on the basis of Date only

I need to find the difference in days between two dates.

For example:

Input: **startDate** = 12-31-2012 23hr:59mn:00sec, **endDate** = 01-01-2013 00hr:15mn:00sec

Expected output: 1

I tried the following:

  1. (dt1-dt2).TotalDays and convert to integer but didn't give me appropriate answer as double has to be converted to int - tried Math.Ceiling, Convert.To...
  2. dt1.day - dt2.day does not work across months
  3. dt.Substract() has the same output as option 1 mentioned above.

None of the above worked, so I ended up writing the following code. The code works well, but I feel that there must be a solution with only a couple of lines of code.

public static int GetDifferenceInDaysX(this DateTime startDate, DateTime endDate)
    {
        //Initializing with 0 as default return value
        int difference = 0;

        //If either of the dates are not set then return 0 instead of throwing an exception
        if (startDate == default(DateTime) | endDate == default(DateTime))
            return difference;

        //If the dates are same then return 0
        if (startDate.ToShortDateString() == endDate.ToShortDateString())
            return difference;

        //startDate moving towards endDate either with increment or decrement
        while (startDate.AddDays(difference).ToShortDateString() != endDate.ToShortDateString())
        {
            difference = (startDate < endDate) ? ++difference : --difference;
        }

        return difference;
    }

Note: I do not have any performance issue in the while-loop iteration as the max difference will not be more than 30 to 45 days.

Upvotes: 25

Views: 48829

Answers (3)

Habib
Habib

Reputation: 223392

Use TimeStamp. Just subtract two dates (using DateTime.Date property), get the difference in time span and return TotalDays

TimeSpan ts = endDate.Date - startDate.Date;
double TotalDays = ts.TotalDays;

So your extension method can be as simple as:

public static int GetDifferenceInDaysX(this DateTime startDate, DateTime endDate)
    {
      return (int) (endDate.Date - startDate.Date).TotalDays;
      // to return just a int part of the Total days, you may round it according to your requirement
    }

EDIT: Since the question has been edited, you may check the following example. Consider the following two dates.

DateTime startDate = new DateTime(2012, 12, 31, 23, 59, 00);
DateTime endDate = new DateTime(2013, 01, 01, 00, 15, 00); 

You can write the extension method as:

public static int GetDifferenceInDaysX(this DateTime startDate, DateTime endDate)
    {
        TimeSpan ts = endDate - startDate;
        int totalDays = (int) Math.Ceiling(ts.TotalDays);
        if (ts.TotalDays < 1 && ts.TotalDays > 0)
            totalDays = 1;
        else
            totalDays = (int) (ts.TotalDays);
        return totalDays;
    }

For the above dates it will give you 1

Upvotes: 3

Damien_The_Unbeliever
Damien_The_Unbeliever

Reputation: 239814

Well, it sounds like you want the difference in the number of days, ignoring the time component. A DateTime with the time component reset to 00:00:00 is what the Date property gives you:

(startDate.Date - endDate.Date).TotalDays

Upvotes: 64

Amiram Korach
Amiram Korach

Reputation: 13296

If you use the DateTime.Date property this will eliminate the time

date1.Date.Subtract(date2.Date).Days

Upvotes: 12

Related Questions