0

Not sure how to approach this. I have an app where users can select a time range (say 5:00 PM to 10:00 AM) that repeats daily based on the days selected by the user (say Mon, Thurs, and Sat). How can I determine if the current time is within one of these ranges?

Edit:

I took a look at Find if current time falls in a time range, but I don't think this is a duplicate because it doesn't address the issue I am getting confused on. For example, I'm not sure what to do when a time range falls into the next day. Like in my example, how would I handle 5:00 PM to 10:00 AM the next day? What about when it falls into the next week like from Sat to Sun (which would be 6 to 0)?

More Info:

I am not working with DateTime objects, I am working with the DaysOfWeek enum and and Timespans to represent the time. Below is a representation of the ranges I would be working with:

| startDay | startTime | endDay |  endTime  |
|----------|-----------|--------|-----------|
|    1     | 17:00:00  |   2    | 09:00:00  |
|    3     | 09:00:00  |   3    | 17:00:00  |
|    6     | 17:00:00  |   0    | 09:00:00  |
Community
  • 1
  • 1
user2628438
  • 169
  • 1
  • 2
  • 8
  • How do you represent your days? Strings, enum, what? – Neil Smith Jul 16 '14 at 20:11
  • I can use either, was planning on just using the DaysOfWeek enum – user2628438 Jul 16 '14 at 20:23
  • This isn't a duplicate. You might want to reword your question with more info about how you represent days so that people don't think you're working with plain ol' DateTimes which the other question addresses. I too thought it was a duplicate until I saw your response to the answer you received. – Neil Smith Jul 16 '14 at 20:28
  • What do you mean by "Timespans to represent the time"? Does one `TimeSpan` represent `startTime` *and* `endTime`? – Peter Ritchie Jul 16 '14 at 22:38
  • I mean one TimeSpan represents startTime, and one TimeSpan represents endTime. – user2628438 Jul 17 '14 at 03:29

1 Answers1

1

Assuming a Sunday start-of-week:

var now = DateTime.Now;

// offset from "Sunday"
var i = now.Date.AddDays(-(double) now.DayOfWeek);
// normalize to this week
var s = i.AddDays((double) startDay).AddHours(startTime.TotalHours);
// normalize to this week
var e = i.AddDays((double)((endDay < startDay) ? endDay + 7 : endDay)).AddHours(endTime.TotalHours);
Debug.Assert(now >= s && now <= e);
Peter Ritchie
  • 35,463
  • 9
  • 80
  • 98
  • This work for the most part, but there is an issue if the time range starts on Sat and ends on Sun. For example, given the range Sat 17:00 to Sun 10:00, and the current time is Sun 06:00, `i` will equal the Sun of this week, but `s` and `e` equal the Sat and Sun of the following week. How do I account for that? – user2628438 Jul 17 '14 at 18:17
  • @user2628438 That would be a fairly minor change by checking if endDay is less than startDay (see edit); but, that kinda points out that your ranges are restricted to no more than one week in length. If you want more than one week in length, you'll have to do something more than just DayOfWeek. – Peter Ritchie Jul 17 '14 at 18:34
  • This helps, now `e` is greater than `s`, but they are on next week's Sat and Sun, with `now` outside of that range because it is the current Sun. Am I missing something? – user2628438 Jul 17 '14 at 19:06
  • What is `now` in your test? 17-Jul-14? If you have a start of Sat and and end of Sun, then that end Sun will always be the "next" week, given a start-of-week of Sunday. But, the Sat will always be in the "current" week. – Peter Ritchie Jul 17 '14 at 19:39
  • I'm testing with `now` being 20-Jul-14 06:00. Which make `s` be 26-Jul-14 and `e` be 27-Jul-14. So `now` is out of range. – user2628438 Jul 17 '14 at 20:25
  • How can you tell if the "start date" is before or after the date you want to check? – Peter Ritchie Jul 17 '14 at 20:36
  • Not sure I understand your questions? – user2628438 Jul 17 '14 at 21:46
  • I guess I can just add an additional check for next week, like `now.AddDays(7) >= s && now.AddDays(7) <= e` and if one is true then it is in range. Its not ideal but it seems to work. I really appreciate the help Peter! – user2628438 Jul 18 '14 at 19:41