Is time between certain times...

lidds

Well-known member
Joined
Oct 19, 2004
Messages
122
Programming Experience
Beginner
I am trying to figure out how to see the current time is between certain times that the user has entered.

I have the following code which works if the time is between 00:01 and 23:59, however if the user enters a time, such as, 20:00 and 05:00 then the below code does not work. Which I guess is because we are now spanning times that exist within 2 days and not one.

What I want to do is as I said above, detect if the current time is between a timespan of, say, 20:00 to 05:00. So for instance this could be Tuesday 20:00 (8PM) and Wednesday 05:00 (5AM), however I can not figure out how to do this as my users could also enter a time like 14:00 to 18:00 on the same day, so cannot use greater than and less than on the times.

VB.NET:
dim inTimeSpan as boolean = DateTime.Now.TimeOfDay >= CISData.Instance.ThreadStart AndAlso DateTime.Now.TimeOfDay <= CISData.Instance.ThreadEnd

Any help would really be appreciated.

Thanks

Simon
 
If the last time is less than first then you can add one day to it, before subtracting the first.
For Date values:
        If last < first Then
            last = last.AddDays(1)
        End If
        Dim time = last - first

or shortened:
Dim time = If(last < first, last.AddDays(1), last) - first

for TimeSpan values:
Dim time = If(last < first, (New Date + last).AddDays(1), last) - first
 
Back
Top