/** * ISO 8601 주차 계산 유틸리티 */ export interface WeekInfo { year: number week: number startDate: Date // 월요일 endDate: Date // 일요일 startDateStr: string // YYYY-MM-DD endDateStr: string } /** * 날짜를 YYYY-MM-DD 문자열로 변환 */ export function formatDate(date: Date): string { const y = date.getFullYear() const m = String(date.getMonth() + 1).padStart(2, '0') const d = String(date.getDate()).padStart(2, '0') return `${y}-${m}-${d}` } /** * 특정 날짜의 ISO 주차 정보 반환 */ export function getWeekInfo(date: Date = new Date()): WeekInfo { const target = new Date(date) target.setHours(0, 0, 0, 0) // 목요일 기준으로 연도 판단 (ISO 규칙) const thursday = new Date(target) thursday.setDate(target.getDate() - ((target.getDay() + 6) % 7) + 3) const year = thursday.getFullYear() const firstThursday = new Date(year, 0, 4) firstThursday.setDate(firstThursday.getDate() - ((firstThursday.getDay() + 6) % 7) + 3) const week = Math.ceil((thursday.getTime() - firstThursday.getTime()) / (7 * 24 * 60 * 60 * 1000)) + 1 // 해당 주의 월요일 const monday = new Date(target) monday.setDate(target.getDate() - ((target.getDay() + 6) % 7)) // 해당 주의 일요일 const sunday = new Date(monday) sunday.setDate(monday.getDate() + 6) return { year, week, startDate: monday, endDate: sunday, startDateStr: formatDate(monday), endDateStr: formatDate(sunday) } } /** * ISO 주차 문자열 반환 (예: "2026-W01") */ export function formatWeekString(year: number, week: number): string { return `${year}-W${week.toString().padStart(2, '0')}` } /** * 지난 주 정보 */ export function getLastWeekInfo(): WeekInfo { const lastWeek = new Date() lastWeek.setDate(lastWeek.getDate() - 7) return getWeekInfo(lastWeek) } /** * 특정 연도/주차의 날짜 범위 반환 */ export function getWeekDates(year: number, week: number): { startDate: Date, endDate: Date } { // 해당 연도의 1월 4일 (1주차에 반드시 포함) const jan4 = new Date(year, 0, 4) // 1월 4일이 포함된 주의 월요일 const firstMonday = new Date(jan4) firstMonday.setDate(jan4.getDate() - ((jan4.getDay() + 6) % 7)) // 원하는 주차의 월요일 const targetMonday = new Date(firstMonday) targetMonday.setDate(firstMonday.getDate() + (week - 1) * 7) // 일요일 const targetSunday = new Date(targetMonday) targetSunday.setDate(targetMonday.getDate() + 6) return { startDate: targetMonday, endDate: targetSunday } }