Replies: 3 comments 1 reply
-
I'm not sure what you mean by "time portion": intervals are specific ranges of time, so you would need to ask about a specific time, (e.g. today at 4:00) as opposed to a standalone-time (4:00 on any day). With that out of the way, this is no problem: const validStart = DateTime.fromISO("2022-07-09T09:00:00", { zone: "America/New_York" })
const validEnd = DateTime.fromISO("2022-07-09T16:00:00", { zone: "America/New_York" })
const testTime = DateTime.now()
// option 1
testTime > validStart && testTime < validEnd
// option 2
const i = Interval.fromDateTimes(validStart, validEnd)
i.contains(testTime) |
Beta Was this translation helpful? Give feedback.
0 replies
-
I'm saying the schedule is the same for any given day. So, how to determine if a given DateTime falls within the local time of, for example, 1400 to 1600. |
Beta Was this translation helpful? Give feedback.
0 replies
-
I would do it like this: const testTime = DateTime.now()
.setZone("America/New_York")
.set({ year: validStartTime.year, month: validStartTime.month, day: validStartTime.day });
// then compare as above |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I'm finding it pretty hard to solve this problem that doesn't seem like it would be all that rare, but nevertheless I don't see an obvious (easy) way to do it. Here's the need:
Some electricity rate plans are "time-of-use," meaning the rate charged depends on the (local) time of day, and sometimes depends on the day of week (weekend or weekday, usually). I'd like to be able to determine if a particular DateTime’s (e.g. "now") time portion falls within the time portion of an Interval specified in a particular time zone.
Note that doing this should be possible regardless of the system time zone on the computer running the code (a lot of APIs I've looked at seem to only work based on the system time zone).
I can't seem to find an elegant way to do this in a robust way.
Beta Was this translation helpful? Give feedback.
All reactions