I use moment.js for time comparison
My usecase
Class have class availability start & end time
User selected time
const selectedTime= moment(userSelectedTime)
Given start and end time,class length is a minute of class like 60 equal 1 clock
const startSelectedTimes = moment(availableLength.start) const endSelectedTimes = moment(availableLength.end).subtract(class.length,'minutes')
1 Check
if user selected time is before start time then it throw a error
if(startTimes.isBefore(startSelectedTimes)){ throw error... return }
2 Check
if user selected time is after end time then it throw a error
if(startTimes.isAfter(endSelectedTimes)){ throw error... return }
After some booked classes your have array of booked class with start and end time
Booked class start & end time loop throw i is loop variable
const classStartTime = moment(i.start) const classEndTime = moment(i.end) const checkBack = moment(i.start).subtract(class.length,"minutes") const checkNext = moment(i.end).add(class.length,"minutes")
3 Check
if the selected time is between start & end time then throw error
if (startTimes.isBetween(classStartTime, classEndTime)===true) { throw error... return ; }
4 Check
Subtract class length to start time then check there is a class between selected time then throw error
if(startTimes.isBetween(checkBack,classStartTime)===true){ throw error... return ; }
5 Check
Add class length to end time and then check if there is a class between selected time then throw error
if(startTimes.isBetween(classEndTime,checkNext)===true){ throw error.. return ; }
finally
Book class
Your logic here
Top comments (0)