SiteMap  | About    

How to find date difference in VB.NET

DateTime Structure Represents an instant in time, typically expressed as a date and time of day. In VB.Net by calling any of the overloads of the DateTime constructor that allow you to specify specific elements of the date and time value like year , month or day.

  Dim newDate As New DateTime(2000, 5, 1)

Here newDate represents year as 2000 and month as May and date as 1 .

A calculation using a DateTime structure, such as Add or Subtract, does not modify the value of the structure. Instead, the calculation returns a new DateTime structure whose value is the result of the calculation. In VB.Net DateTime.Add returns a new DateTime that adds the value of the specified TimeSpan to the value of this instance. The DateTime.Substract method may be used in order to find the date-time difference between two instances of the DateTime method.

  Dim diff As System.TimeSpan = secondDate.Subtract(firstDate)

You can also find the difference between two dates using the following method.

  Dim diff2 As String = (secondDate - firstDate).TotalDays.ToString()

Public Class Form1

	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		Dim firstDate As New System.DateTime(2000, 1, 1)
		Dim secondDate As New System.DateTime(2000, 5, 31)

		Dim diff As System.TimeSpan = secondDate.Subtract(firstDate)
		Dim diff1 As System.TimeSpan = secondDate - firstDate

		Dim diff2 As String = (secondDate - firstDate).TotalDays.ToString()

	End Sub
End Class (C) 2016    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.