'How to check if time overlaps in javascript
Hi i am working on Reactjs Application where user needs to select four time slots .Like
slot1 starting time, slot2 starting time ,slot1 ending time ,slot2 ending time. I need to check that the slot1 time and slot2 time should not overlap each other. And Starting time of each slot should be greater than of that slot end time
Like if user selects
slot1 time 10:00 AM- 12:00PM
slot2 time 8:00Am - 9:00 AM.
it should works in this case
const chec = () => {
var a = moment(slot1StartTime).format("hh:mm a");
var b = moment(slot1EndTime).format("hh:mm a");
var c = moment(slot2StartTime).format("hh:mm a");
var d = moment(slot2EndTime).format("hh:mm a");
var diff = moment
.duration(moment(a, "hh:mm a").diff(moment(b, "hh:mm a")))
.asMinutes();
var diff2 = moment
.duration(moment(c, "hh:mm a").diff(moment(d, "hh:mm a")))
.asMinutes();
var diff3 = moment
.duration(moment(b, "hh:mm a").diff(moment(c, "hh:mm a")))
.asMinutes();
if (diff > 0 || diff2 > 0 || diff3 > 0) {
console.log("error");
}
};
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
