Skip to content

Instantly share code, notes, and snippets.

@rdundon
Last active February 19, 2020 22:05
Show Gist options
  • Save rdundon/c2d6414974e6c9f5ab155ebf2c408179 to your computer and use it in GitHub Desktop.
Save rdundon/c2d6414974e6c9f5ab155ebf2c408179 to your computer and use it in GitHub Desktop.
/**
* Little script/function to get time intervals
*/
// Based on:
// https://stackoverflow.com/a/19225446/808870
const diffInHours = (startDate, endDate) => ( endDate.valueOf() - startDate.valueOf() ) / 1000 / 60 / 60;
const getTimeIntervalsPerHour = interval => {
var times = [];
var i =0;
while (i < 60) {
times.push(i);
i = i + interval;
}
return times;
}
const getAvailableTimeSlots = (startTime, endTime, minuteInterval) => {
var availableTimeSlots = [];
var currentIntervalTime = startTime;
while (currentIntervalTime < endTime) {
var minuteIntervalInMilliseconds = minuteInterval * 60 * 1000;
availableTimeSlots.push(currentIntervalTime);
currentIntervalTime = new Date(currentIntervalTime.getTime() + minuteIntervalInMilliseconds);
}
return availableTimeSlots;
}
const startTime = new Date('2020-02-19 11:00 EST');
const endTime = new Date('2020-02-19 21:00 EST');
const minuteInterval = 15;
const numberOfHoursOpen = diffInHours(startTime, endTime);
console.log(getAvailableTimeSlots(startTime, endTime, minuteInterval));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment