Skip to content

Instantly share code, notes, and snippets.

@nicksheffield
Created February 17, 2020 02:49
Show Gist options
  • Save nicksheffield/d02f9ee575c3b2d38a1ca49e39425a82 to your computer and use it in GitHub Desktop.
Save nicksheffield/d02f9ee575c3b2d38a1ca49e39425a82 to your computer and use it in GitHub Desktop.
Generate an array of time slots from a beginning to an end, skipping forward a certain amount of time each time
import { isBefore, setHours, setMinutes, setSeconds, addMinutes, setMilliseconds } from 'date-fns'
const setTime = (x, h = 0, m = 0, s = 0, ms = 0) => setHours(setMinutes(setSeconds(setMilliseconds(x, ms), s), m), h)
const from = setTime(new Date(), 9)
const to = setTime(new Date(), 17)
const step = (x) => addMinutes(x, 30)
const blocks = []
let cursor = from
while(isBefore(cursor, to)) {
blocks.push(cursor)
cursor = step(cursor)
}
console.log(blocks)
@jordizle
Copy link

jordizle commented Jun 4, 2023

Thanks @MEGApixel23

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment