I am trying to compare only time (regardless of the date) in js. First I need a way to compare times so I came across the luxon library which works well.
But, there is a problem when comparing intervals across midnight. For example: 20:00 < 01:00 < 10:00
should evaluate to true, the luxon’s library returns a false. Which makes sense because 20:00 < 01:00
can evaluate differently when in different intervals. These is what I would be expecting:
20:00 < 01:00 < 10:00 == true // but it evaluates as false using luxon's interval
00:00 < 01:00 < 10:00 == true
This is the code I have so far:
const from = DateTime.fromISO('20:00');
const to = DateTime.fromISO('10:00');
const value = DateTime.fromISO('01:00');
const interval = Interval.fromDateTimes(from, to);
console.log(interval.contains(valueTime)); // returns false instead of true
2
Answers
Substract on day from
from
, iffrom
is bigger thanto
.And substract on day from
value
, if it is bigger thanto
, to get it into the interval.Testing different values:
Seems to be the desired behavior.
You need the date unless the to is always smaller than the from across midnight