Subtract two datetimepicker values

RipIT

Member
Joined
Apr 5, 2010
Messages
5
Programming Experience
Beginner
Hello
I have two datetimepicker controls one as a start date the other as end date,
How do I go about subtracting the values to ge the number of hours?
I tried...

Private Sub CalculateHours()
Dim TotalHours As Integer
Dim StartDate As Date
Dim FinishDate As Date

StartDate = DateTimePicker1.Value
FinishDate = DateTimePicker2.Value
TotalHours = 24 * (FinishDate - StartDate) ' error here

End Sub

...but I get the error "Operator '*' is not defined fpr types integer and System.timespan"

Do I need to do some casting?
thanks Steve
 
When you subtract two DateTime objects you get a TimeSpan object. Using that TimeSpan object you can get the number of hours/days/mins/seconds....
 
Hello
I changed stratergy a bit and tried...

Private Sub CalcTimeButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalcTimeButton.Click
TextBox.Text = CType(DateTimePicker1.Value - DateTimePicker1.Value, TimeSpan).ToString
End Sub

but I get 00:00:00
 
oops sorry for the cross thread.
This worked for the hours...

TextBox.Text = DateDiff(DateInterval.Hour, DateTimePicker1.Value, DateTimePicker2.Value)

thanks for the help
 
Back
Top