Skip to content

Instantly share code, notes, and snippets.

@saitergun
Created June 5, 2023 09:49
Show Gist options
  • Save saitergun/2561593a924a923d52d9c31826efb84c to your computer and use it in GitHub Desktop.
Save saitergun/2561593a924a923d52d9c31826efb84c to your computer and use it in GitHub Desktop.
create session times with between two shift times
import moment from 'moment'
function createSessionsGroups(duration: number, shiftStart: moment.Moment, shiftEnd: moment.Moment, initialSessions: Array<string> = []): Array<string> {
const sessions: Array<string> = [...initialSessions]
const sessionStart = moment().hour(shiftStart.hour()).minute(shiftStart.minute())
const sessionEnd = sessionStart.clone().add(duration, 'minute')
sessions.push(sessionStart.format('HH:mm'))
const isBefore = sessionEnd.isBefore(shiftEnd)
if (isBefore) {
return createSessionsGroups(duration, shiftStart.add(duration, 'minute'), shiftEnd, sessions)
}
return sessions
}
const timeSlots = [
{
start: '09:00:00.0000000',
end: '18:00:00.0000000',
},
]
let sessions: Array<string> = []
timeSlots.forEach(({ start, end }) => {
const duration = 15
const startTime = moment(start, 'hh.mm.ss.sssssss')
const endTime = moment(end, 'hh.mm.ss.sssssss')
const sesions = createSessionsGroups(duration, startTime, endTime)
sessions.push(...sesions)
})
// output: [
// '09:00',
// '09:15',
// '09:30',
// '09:45',
// '10:00',
// '10:15',
// '10:30',
// '10:45',
// '11:00',
// '11:15',
// '11:30',
// '11:45',
// '12:00',
// '12:15',
// '12:30',
// '12:45',
// '13:00',
// '13:15',
// '13:30',
// '13:45',
// '14:00',
// '14:15',
// '14:30',
// '14:45',
// '15:00',
// '15:15',
// '15:30',
// '15:45',
// '16:00',
// '16:15',
// '16:30',
// '16:45',
// '17:00',
// '17:15',
// '17:30',
// '17:45',
// ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment