Created
February 9, 2017 16:11
-
-
Save digitalWestie/354b036c0714ed69427244d95e8902e9 to your computer and use it in GitHub Desktop.
Momentjs function to check whether a time fits within a set time range, in hours and minutes, excluding dates.
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
| /* | |
| Dates are ignored, only tests moment objects are within a time range of hours and minutes. | |
| Supports times past midnight e.g. 01:00 is in between 23:00 and 03:00 etc. | |
| */ | |
| var withinTime = function(start, end, t) { | |
| //normalise year/month/day | |
| var now = moment(); | |
| start.set({ year: now.year(), month: now.month(), day: now.day(), seconds: 0 }); | |
| end.set({ year: now.year(), month: now.month(), day: now.day(), seconds: 0 }); | |
| t.set({ year: now.year(), month: now.month(), day: now.day(), seconds: 0 }); | |
| var result = false; | |
| if (start > end){ | |
| result = ((t <= end) || (t >= start)); | |
| } else { | |
| result = ((t >= start) && (t <= end)); | |
| } | |
| return result; | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment