import {
addDays,
addMonths,
eachDayOfInterval,
eachWeekOfInterval,
startOfMonth,
} from 'date-fns'
const startOfMonthDate = startOfMonth(new Date())
const matrix = eachWeekOfInterval({
start: startOfMonthDate,
end: addMonths(startOfMonthDate, 1),
}).map((weekDay) => {
const startDate = new Date(weekDay)
return eachDayOfInterval({
start: startDate,
end: addDays(startDate, 6),
})
})
캘린더 이벤트 겹침 처리 알고리즘
dayjs로 특정 날짜가 포함된 주의 모든 날짜 가져오기
import dayjs from 'dayjs'
import isoWeek from 'dayjs/plugin/isoWeek'
dayjs.extend(isoWeek)
// startOf('week') → 일요일 시작
// startOf('isoWeek') → 월요일 시작 (캘린더 UI에 주로 사용)
function getWeekDays(date, iso = true) {
const start = dayjs(date).startOf(iso ? 'isoWeek' : 'week')
return Array.from({ length: 7 }, (_, i) =>
start.add(i, 'day').format('YYYY-MM-DD')
)
}
getWeekDays('2025-10-28') // ['2025-10-27', ..., '2025-11-02']