How do I calculate the difference between two dates to display as a number in days?
I have 2 text boxes (txtHStart_Date & txtHEnd_Date) I have used an Ajax Calendar Extender to enter the dates in each of these text boxes.
I would like to get the difference between these two dates to show in a seperate text box (txtNoOfDays)
I’ve seen the timespan function but can seem to get this to work.
I’m not to sure how to declare the text boxes as the dates I would the calculation to be made from
Code:
Dim D1 As Date
Dim D2 As Date
Dim ts As New TimeSpan
D1 = txtHStart_Date.Text
D2 = txtHEnd_Date.Text
ts = D2 - D1
But I know this isn’t right. I also don’t know how to get it to display in the 3rd TextBox.
The
TimeSpanis simple to use.Edit:
And going by the comment you added where you assign a text string to a date variable, you would be better off using (and handling)
TryParsereturns a boolean success/fail so you can react to whether you were given good/bad data.