-6

I want to find difference between two DateTime objects. I know I can use DateTime.Subtract() and overloaded operator '-' (e.g DateTime - DateTime). But someone told me that these two techniques are not good enough, and there is a method DateDiff or something, which I should use and which is best to use. I want to know if it is true? If it is, then how the above two are inferior from any perspective?

Muhammad Fahad Nadeem
  • 1,120
  • 1
  • 9
  • 15
  • 3
    `But someone told me that these two techniques are not good enough` They lied. Unless this is in the context of something very specific (e.g. EF) in which case https://learn.microsoft.com/en-us/dotnet/api/system.data.entity.sqlserver.sqlfunctions.datediff?view=entity-framework-6.2.0 may be worth a read. – mjwills Jul 18 '19 at 12:16
  • 1
    `DATEDIFF` is a [T-SQL function](https://learn.microsoft.com/en-us/sql/t-sql/functions/datediff-transact-sql?view=sql-server-2017) that has nothing to do with C#. If you hear people claiming a certain technique is superior or inferior to another, ask them to back up such claims with facts and documentation. If they can't, ignore them. This claim _might_ make some sense in the context of Entity Framework queries, but then still, more context and proof is required. – CodeCaster Jul 18 '19 at 12:17
  • @CodeCaster There is also https://learn.microsoft.com/en-us/dotnet/api/microsoft.visualbasic.dateandtime.datediff?view=netframework-4.8 . But again, I can't imagine why people would say it should be used. – mjwills Jul 18 '19 at 12:20
  • Please show us two date inputs and the **result you want** from diffing those two dates. That will help us better understand your problem. – mjwills Jul 18 '19 at 12:42
  • https://stackoverflow.com/a/56968736 – TonyMkenu Jul 18 '19 at 12:51

2 Answers2

2

DateDiff does not compare dateTime instances.
It compares specific parts of the DateTime instances - for example, the months, or hours.

For instance:

var lasyDayOf2018 = new DateDime(2018, 12, 31);
var firstDayOf2019 = lasyDayOf2018.AddDays(1);
var monthDiff = DateDiff(DateInterval.Month, lasyDayOf2018, firstDayOf2019)

monthDiff will be 1, even though only one day has passed between the two dates.

It's documented in the Remarks section of the DateDiff documentation page:

If Interval is set to DateInterval.Year, the return value is calculated purely from the year parts of Date1 and Date2. Similarly, the return value for DateInterval.Month is calculated purely from the year and month parts of the arguments, and for DateInterval.Quarter from the quarters containing the two dates.

For example, when comparing December 31 to January 1 of the following year, DateDiff returns 1 for DateInterval.Year, DateInterval.Quarter, or DateInterval.Month, even though at most only one day has elapsed.

To get the actual difference between different instances of DateTime, either use Subtract or -, as you wrote in the question.

Community
  • 1
  • 1
Zohar Peled
  • 79,642
  • 10
  • 69
  • 121
1

Whether or not there is a problem, and the solution to that problem really depends on the situation and why you're computing the difference. There are two main problems with DateTime.Subtract() and operator -.

The first is that they both ignore the time zone portion of the data. So, for example, if you have two dates, 2019-07-18 12:00:00 UTC and 2019-07-18 06:00:00 MDT (local time), even though they represent the same moment in time (as measured from different time zones), DateTime.Subtract() will tell you that they're 6 hours apart.

The second problem is that you often want to know the answer in terms of units other than ticks, sometimes even something that cannot be converted to a fixed number of ticks. For example, if you want the difference in the months represented by two DateTimes, you cannot compute that from the difference in ticks (which is what DateTime.Subtract() is based on), because different months have different lengths. For example, for the dates '2019-06-30 23:59:59' and '2019-07-01 00:00:00' (in the same time zone) there is only a one second difference, but the months are different, so is the difference in months one or zero? It depends on what you want to use the answer for.

James
  • 3,551
  • 1
  • 28
  • 38