DateDiff for time

jamie_pattison

Well-known member
Joined
Sep 9, 2008
Messages
116
Programming Experience
Beginner
Is it possible to use DateDiff to find the difference between time?

I have two time periods i.e. 05:00 and 05:10pm so i would like to run some code if the time is between the two specified periods. Ive searched around and i think DateDiff could do this but unsure?

Thanks
 
There's no need to use DateDiff whether it can do it or not. Both the DateTime and TimeSpan data types support mathematical operations so you can perform comaprisons, subtractions, etc. E.g. if you want to see if the current time is between 17:00 and 17:10
VB.NET:
Dim startTime As New TimeSpan(17, 0, 0)
Dim endTime As New TimeSpan(17, 10, 0)
Dim currentTime As TimeSpan = Date.Now.TimeOfDay

If currentTime >= startTime AndAlso currentTime <= endTime Then
    'We are within the time interval.
End If
 
Back
Top