I’ve been using the following timespan function to work out the number of days between two dates which works correctly.
Dim dtStart As Date = txtHStart_Date.Text
Dim dtEnd As Date = txtHEnd_Date.Text
Dim ts As TimeSpan = dtEnd - dtStart
txtNoofDays.Text = ts.TotalDays.ToString()
Console.WriteLine(ts.TotalDays)
I’ve now tried to add a check box (as a half day selection). If the check box is selected I want it to minus 0.5 off the total days. But i’m getting the blue line telling me its an error on “ts = (dtEnd – dtStart) – 0.5”
Dim dtStart As Date = txtHStart_Date.Text
Dim dtEnd As Date = txtHEnd_Date.Text
Dim ts As TimeSpan = dtEnd - dtStart
For Each li As ListItem In CheckBoxList1.Items
If li.Value = "Half Day" Then
ts = (dtEnd - dtStart) - 0.5
Else
ts = dtEnd - dtStart
End If
Next
txtNoofDays.Text = ts.TotalDays.ToString()
Console.WriteLine(ts.TotalDays)
Any suggestions on how to correct
tsis yourTimeSpanvalue. You can’t subtract 0.5 from aTimeSpan– I know what you want it to mean in this case, but why would “days” be the right unit by default? You can subtract 0.5 days explicitly though:It’s not clear why you’re iterating over all the items but only actually using the last item, by the way. If your plan was actually to use the number of days for each item, I’d be tempted to do the subtraction after working out the number of days: