Check if time falls in between specified range.


I’m working on an app where I want to compare a list of time’s to the current time and set a value if the time is within the range.

I’m current storing the times in a preference file on OS X and reading them when the app launches. I’m using the MBS plugins for CFPreference calls. I currently have the times stored in an array in a plist:


I want to check the current time and set the variable ‘App.ContinueThread = true’ if the current time is within the range of those times.

I’ve created an array of the times from the plist and currently just have this code to determine the current hour/minute:
Dim today As New Date()
Dim currentHour As String = Format(today.Hour, “00”)
Dim currentMinute As String = Format(today.Minute, “00”)

This provides me with a nicely formatted time like so: “15:11”

I’m looking for advice on how to compare the current time to the time ranges in the array. I suspect I’ll need to loop through all the ranges, splitting the beginning and ending times, then comparing them to the current time.

What’s the best way to handle this? I’m also willing to look at different ways to store the exception time ranges if that helps.


When you store the ranges, does it have to be human-readable? If not, I’d store them as seconds since midnight instead. Calculating that for the current time is pretty easy, and will give you a convenient way to compare it to the ranges.

Just remember to do extra logic if the end time of a range is less than the start time, for example, 23:30-00:30.

Kem, Human readable is preferred, as we’d like to generate reports based on what’s in the plist. As I think about it, our reports may be able to do the calculations, but it could get messy. Seconds since midnight sounds like a good path otherwise.

Yes, seconds since midnight will work either way.

Perhaps I can just change the time ranges to seconds from midnight when I read the variables? That way I can store them human readable.

I could split the hour:minute up and calculate the total minutes combined:

00:00 = 0060 + 0060 = 0 //beginning time frame
00:30 = 0060 + 3060 = 1800 // seconds past midnight

then check if the current time is between 0 and 1800 seconds from midnight?

h * 3600 + m * 60, but yes, that’s it. If it will only ever be hours/minutes then you can use minutes since midnight, h * 60 + m.

OMG. 3600! DUH. A long monday. Yes, that’s what I’ll want! :slight_smile:

Thanks Kem!