Difference between two dates in VB.NET
To find the difference between two dates, we can use DateTime.Subtract method.
To find the difference between two dates, we can use DateTime.Subtract method. The DateTime.Subtract method takes a DateTime object and subtracts it from a DateTime and returns result as a TimeSpan object.
Here is an example that demonstrates how to subtract a date and display results on the console.
Module Module1
Sub Main()
' Start date
Dim startDate As DateTime = New DateTime(2005, 2, 1, 3, 4, 12, 56)
' End date
Dim endDate As DateTime = New DateTime(2005, 12, 12, 4, 30, 45, 12)
' Time span
Dim diffDate As TimeSpan = endDate.Subtract(startDate)
' Spit it out
Console.WriteLine("Time Difference: ")
Console.WriteLine(diffDate.Days.ToString() + " Days")
Console.WriteLine(diffDate.Hours.ToString() + " Hours")
Console.WriteLine(diffDate.Minutes.ToString() + " Minutes")
Console.WriteLine(diffDate.Seconds.ToString() + " Seconds ")
Console.WriteLine(diffDate.Milliseconds.ToString() + " Milliseconds ")
Console.ReadKey()
End Sub
End Module