How to get the difference between two dates in .net

You can use the built-in function for getting the difference between two dates.

Example:

  1.         Dim d1 As DateTime = Now
  2.         Dim d2 As DateTime = Now.AddDays(3)
  3.  
  4.         Dim m As TimeSpan = d2.Subtract(d1)
  5.         Dim t As String
  6.  
  7.         t = "Days : " & m.Days.ToString() & vbCrLf
  8.         t += "Hours : " & m.Hours.ToString() & vbCrLf
  9.         t += "Minutes : " & m.Minutes.ToString() & vbCrLf
  10.         t += "Seconds : " & m.Seconds.ToString() & vbCrLf
  11.         t += "Milliseconds : " & m.Milliseconds.ToString() & vbCrLf
  12.         MessageBox.Show(t)

, ,

  1. No comments yet.
(will not be published)
Submit Comment

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Subscribe to comments feed
  1. No trackbacks yet.

SetPageWidth