Created
November 17, 2017 17:48
-
-
Save krohne/b7763ff05a5fed9a15d271a47e9d8f16 to your computer and use it in GitHub Desktop.
Check if time range excludes a time
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // You could play around with allowed inputs | |
| import Moment from 'moment'; | |
| import { extendMoment } from 'moment-range'; | |
| const moment = extendMoment(Moment); | |
| function excludesTime(start, end, when) { | |
| let startTime = moment(start, 'HH:mm'); | |
| const endTime = moment(end, 'HH:mm'); | |
| const whenTime = moment(when, 'HH:mm'); | |
| if (startTime.diff(endTime) > 0) { // in case start/end crosses midnight | |
| startTime = moment(startTime).add(-1, 'day'); | |
| } | |
| return ! moment.range(startTime, endTime).contains(whenTime); | |
| } | |
| const start = '23:00'; | |
| const end = '01:00'; | |
| const restricted = '00:30'; | |
| const lateEnough = '01:30'; | |
| const earlyEnough = '23:30'; | |
| console.log(moment(start, 'HH:mm').format('hh:mma'), 'to', moment(end, 'HH:mm').format('hh:mma'), 'restricted time period', ); | |
| console.log(moment(restricted, 'HH:mm').format('hh:mma'), 'allowed?', excludesTime(start, end, restricted)); // false | |
| console.log(moment(lateEnough, 'HH:mm').format('hh:mma'), 'allowed?', excludesTime(start, end, lateEnough)); // true | |
| console.log(moment(earlyEnough, 'HH:mm').format('hh:mma'), 'allowed?', excludesTime(start, end, earlyEnough)); // trude |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment