Skip to content

Instantly share code, notes, and snippets.

@digitalWestie
Created February 9, 2017 16:11
Show Gist options
  • Select an option

  • Save digitalWestie/354b036c0714ed69427244d95e8902e9 to your computer and use it in GitHub Desktop.

Select an option

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.
/*
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