Posted by: Duncan Mackenzie, MSDNThis 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()
'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 Days4222 Hours253345 Minutes15200730 Seconds
TimeSpan
175.934383644387 Days4222.42520746528 Hours253345.512447917 Minutes15200730.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:
Read the corresponding documentation for more information, but I hope that this has been helpful...