-
-
Save PaulSebalu/8622b2683a3488e375caaeed62762115 to your computer and use it in GitHub Desktop.
Get times in between two timestamps with JavaScript.
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
/** | |
* Made a few tweaks from Miguel's original code to... | |
* meet my usecase. | |
*/ | |
import dayjs from 'dayjs'; | |
import utc from 'dayjs/plugin/utc'; | |
import customParseFormat from 'dayjs/plugin/customParseFormat'; | |
import timezone from 'dayjs/plugin/timezone'; | |
dayjs.extend(utc); | |
dayjs.extend(timezone); | |
dayjs.extend(customParseFormat); | |
function getAvailabilitySlots(startTime, endTime) { | |
const slots = []; | |
let start = startTime; | |
const addDays = function (minutes) { | |
const currentTime = new Date(this.valueOf()); | |
currentTime.setMinutes(currentTime.getMinutes() + minutes); | |
return currentTime; | |
}; | |
while (start <= endTime) { | |
slots.push(start); | |
start = addDays.call(start, 15); | |
} | |
return slots; | |
} | |
// Usage | |
const slots = getAvailabilitySlots( | |
new Date(new Date().setUTCHours(9, 0, 0, 0)), | |
new Date(new Date().setUTCHours(17, 0, 0, 0)) | |
); | |
slots.forEach(function (date) { | |
console.log(dayjs(date).local().format()); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment