Determine if Now is between specific day/time range (app downtime)

Consider I have a spreadsheet like this:

image

I need to come up with a way to determine if the current date and time is within one of those specified ranges.

This is to perform an automatic stop at certain times if a Job is running. I’ll do this within the code for the Process. Trying to come up with a standardized way of doing this.

The tricky part seems to be ranges that fall across midnight so the Start Day and End Day are different. I suppose these could be broken up into two ranges, so the first entry would turn into…

image

If your suggestion includes a different way of storing the stop/start ranges, that’s fine too.

OK so I figured it out. First off, it’s easier if you spell out the days so Saturday instead of Sat etc.

The key was finding this expression: Now.TimeOfDay.TotalMinutes which gives the total number of minutes today since midnight.

Then it was a simple matter of two branches - one if the Start Day and End Day are the same, and another if they’re different:

If Start Day and End Day are the same, check:

  • is today the same as the Start Day
  • AND is Now.TimeOfDay.TotalMinutes > Start Time (also in TimeOfDay.TotalMinutes)
  • AND is Now.TimeOfDay.TotalMinutes < End Time (also in TimeOfDay.TotalMinutes)

Now.DayOfWeek.ToString.ToUpper = CurrentRow("Start Day").ToString.ToUpper AND Now.TimeOfDay.TotalMinutes > DateTime.Parse(CurrentRow("Start Time").ToString).TimeOfDay.TotalMinutes AND Now.TimeOfDay.TotalMinutes < DateTime.Parse(CurrentRow("End Time").ToString).TimeOfDay.TotalMinutes

If Start Day and End Day are different, check:

  • if Now.DayOfWeek is the same as Start Day
  • AND Now.TimeOfDay.TotalMinutes > Start Time
  • OR
  • Now.DayOfWeek is the same as End Day
  • AND Now.TimeOfDay.TotalMinutes < End Time

(Now.DayOfWeek.ToString.ToUpper = CurrentRow("Start Day").ToString.ToUpper AND Now.TimeOfDay.TotalMinutes > DateTime.Parse(CurrentRow("Start Time").ToString).TimeOfDay.TotalMinutes) OR (Now.DayOfWeek.ToString.ToUpper = CurrentRow("End Day").ToString.ToUpper AND Now.TimeOfDay.TotalMinutes < DateTime.Parse(CurrentRow("End Time").ToString).TimeOfDay.TotalMinutes)

With some sample data, the output looks like:

image

I’ll just throw a break in both “Now is within” results, so once it finds a matching range it just stops. No reason to keep looking.

image

image

Note that this would not account for a range that spans multiple says (ie Friday 5pm through Sunday 5am) but we don’t expect to need that.

In the end, I suppose all three If conditions could be combined into one. Makes for a complicated statement, though.