Zhen Chua Zhen Chua - 1 month ago 10
Vb.net Question

days gap between 2 dates

i am facing problem when i'm getting difference between 2 dates. For example, '9/11/2016' minus '7/11/2016' the result comes out 62. Why? Anyone can help me please? Any response is appreciated, Thanks in advance. :)

Protected Sub txtReturnDate_TextChanged(sender As Object, e As EventArgs) Handles txtReturnDate.TextChanged

Dim d1 As DateTime = txtArriveDate.Text
Dim d2 As DateTime = txtReturnDate.Text

Dim days As Integer = (d2 - d1).TotalDays
lblDuration.Text = days.ToString()
End Sub

Answer

62 = days between 11 july 2016 and 11 sep 2016. Try below for date conversion. This will avoid the confusion of day vs month.

Dim dt As DateTime = DateTime.ParseExact(txtArriveDate.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture)