How do I determine the difference between two dates?

Posted by: Duncan Mackenzie, MSDN
This post applies to Visual Basic .NET 2002/2003

This common question is often phrased as "How do I find the number of hours between two dates?", substituting minutes, seconds, days, or whatever interval you are looking for in the place of 'hours'. Well, in Visual Basic .NET there are two main ways to achieve this result; the DateDiff function or through the TimeSpan structure.

Both of these methods are equally valid, where they overlap in functionality, but each of them has a couple of unique features.  Despite their similar functions, the core difference between the two is that DateDiff is a function, so you need to call it every time you need to retrieve a value, whereas TimeSpan is a structure that is created once and then you just work with its various members as needed.

Using DateDiff, you call it with different date interval parameters to retrieve the appropriate value:

         Dim D1, D2 As Date
        D1 = Date.Now
        D2 = #11/9/2004#
        'DateDiff
        Console.WriteLine("DateDiff")
        Console.WriteLine()
        Console.WriteLine("{0} Days", _
            DateDiff(DateInterval.Day, D1, D2))
        Console.WriteLine("{0} Hours", _
            DateDiff(DateInterval.Hour, D1, D2))
        Console.WriteLine("{0} Minutes", _
            DateDiff(DateInterval.Minute, D1, D2))
        Console.WriteLine("{0} Seconds", _
            DateDiff(DateInterval.Second, D1, D2))
        Console.WriteLine()

Alternatively, a TimeSpan structure can be retrieved as the result of subtracting one date from another, and then querying the various members of that structure.

         'TimeSpan
        Console.WriteLine("TimeSpan")
        Console.WriteLine()
        Dim difference As TimeSpan = D2.Subtract(D1)
        Console.WriteLine("{0} Days", difference.TotalDays)
        Console.WriteLine("{0} Hours", difference.TotalHours)
        Console.WriteLine("{0} Minutes", difference.TotalMinutes)
        Console.WriteLine("{0} Seconds", difference.TotalSeconds)
        Console.WriteLine()

The output of the two different methods is nearly identical, except that the TimeSpan properties are returning Doubles, while DateDiff always returns Longs (Int64).

DateDiff

175 Days
4222 Hours
253345 Minutes
15200730 Seconds

TimeSpan

175.934383644387 Days
4222.42520746528 Hours
253345.512447917 Minutes
15200730.746875 Seconds

The fractional values returned from the TimeSpan properties are more meaningful that they might seem at first glance; each of these properties is returning the complete difference between the two dates, whereas the whole numbers returned from DateDiff only provide a value to within one interval (day, hour, etc…). Using only one of these properties from TimeSpan, it would be possible to figure out any other time interval (with varying degrees of precision, of course), although that calculation would seldom be required in practice.

The TimeSpan structure provides additional members (.Hours, .Days, etc...) that return values closer to what you see from DateDiff, so you can use those if it is more suitable for your situation. While TimeSpan can appear to have more features and to be more efficient, DateDiff has two benefits that may cause you to continue to use it in your Visual Basic .NET applications:

  1. It provides several date intervals that are not covered by TimeSpan, including Weeks and Quarters, and
  2. It is compatible with the VB6 function of the same name, allowing you to more easily reuse existing code.

Read the corresponding documentation for more information, but I hope that this has been helpful...