Recipes
Pick the task closest to yours, then replace the dates and working hours.
Each example includes its imports and uses an explicit timezone or UTC
timestamps. Fixed dates stay useful for experimenting; the booking-window
example also fixes now so its results don’t change as time passes.
Exclude a lunch break (and other blackout windows)
excludedWindows are subtracted from the base range before slotting. Overlapping
exclusions are merged automatically.
import { generateTimeslots } from 'timeslottr'
generateTimeslots({
day: '2024-03-15',
timezone: 'America/New_York',
range: { start: '09:00', end: '17:00' },
slotDurationMinutes: 30,
excludedWindows: [
{ start: '12:00', end: '13:00' }, // lunch
{ start: '15:00', end: '15:15' }, // afternoon break
],
})Find common availability for a team
Pass one busy list per person. Only times when everyone is free produce slots. Here, the remaining windows are 11:00–14:00 and 15:00–17:00 in New York: ten 30-minute meeting options.
import { generateAvailableTimeslots } from 'timeslottr'
generateAvailableTimeslots({
day: '2024-03-15',
timezone: 'America/New_York',
range: { start: '09:00', end: '17:00' },
slotDurationMinutes: 30,
participantsBusy: [
[{ start: '09:00', end: '11:00' }], // person A
[{ start: '14:00', end: '15:00' }], // person B
],
})Compute free gaps from existing bookings
If you already have booked Timeslots and want the holes between them, use
findGaps.
import { findGaps, createTimeslot } from 'timeslottr'
const booked = [
createTimeslot(new Date('2024-03-15T10:00:00Z'), new Date('2024-03-15T11:00:00Z')),
]
const free = findGaps(booked, {
start: new Date('2024-03-15T09:00:00Z'),
end: new Date('2024-03-15T17:00:00Z'),
})
// result: [09:00 to 10:00, 11:00 to 17:00]Handle daylight-saving days correctly
Pass a timezone and use time-only boundaries. DST transitions are resolved for
you. See Core concepts.
import { generateTimeslots } from 'timeslottr'
generateTimeslots({
day: '2024-11-03', // US fall-back: the 1am to 2am hour happens twice
timezone: 'America/New_York',
range: { start: '00:00', end: '06:00' },
slotDurationMinutes: 30,
}).length // 14: seven real hoursSend slots over the network
JSON stores dates as strings. Use the JSON helpers to restore Date objects
when you read the data back:
import { generateTimeslots, timeslotToJSON, timeslotFromJSON } from 'timeslottr'
const slots = generateTimeslots({
day: '2024-03-15',
timezone: 'UTC',
range: { start: '09:00', end: '10:00' },
slotDurationMinutes: 30,
})
const payload = JSON.stringify(slots.map(timeslotToJSON))
const restored = JSON.parse(payload).map(timeslotFromJSON)
restored[0].start instanceof Date // trueCap how many slots you generate
Use maxSlots to return only the first few options. This example returns eight
15-minute slots, from 09:00–09:15 through 10:45–11:00. With
generateDailyTimeslots, the cap applies separately to each day.
import { generateTimeslots } from 'timeslottr'
generateTimeslots({
day: '2024-03-15',
timezone: 'America/New_York',
range: { start: '09:00', end: '17:00' },
slotDurationMinutes: 15,
maxSlots: 8, // stop after the first 8
})Only offer slots someone can actually book
Allow two hours to prepare and accept bookings less than 60 calendar days ahead of the reference time:
import { generateDailyTimeslots } from 'timeslottr'
generateDailyTimeslots(
{ start: '2024-03-01', end: '2024-06-01' },
{
range: { start: '09:00', end: '17:00' },
slotDurationMinutes: 30,
timezone: 'America/New_York',
now: '2024-03-01T08:00:00-05:00', // fixed clock for this example
minimumNoticeMinutes: 120, // nothing sooner than 2 hours from now
maximumAdvanceDays: 60, // nothing further out than 60 days
},
)Unbookable slots are dropped before maxSlots is applied, so combining the two
counts only bookable slots, up to the available number per day. Omit the fixed
now in a live booking flow to use the current clock. See Booking windows.