Skip to content

Instantly share code, notes, and snippets.

@PaulSebalu
Forked from miguelmota/getDates.js
Last active November 29, 2022 12:52
Show Gist options
  • Save PaulSebalu/8622b2683a3488e375caaeed62762115 to your computer and use it in GitHub Desktop.
Save PaulSebalu/8622b2683a3488e375caaeed62762115 to your computer and use it in GitHub Desktop.
Get times in between two timestamps with JavaScript.
/**
* 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