export const formatDate = (date: Date): string => { if (!date) return ''; const year = date.getFullYear(); const month = String(date.getMonth() + 1).padStart(2, '0'); // 월은 0부터 시작하므로 +1 const day = String(date.getDate()).padStart(2, '0'); return `${year}-${month}-${day}`; }; // 현재 년도부터 10년 전까지의 년도 옵션 생성 export const yearOptions = Array.from({ length: 10 }, (_, i) => { const year = new Date().getFullYear() - i; return { text: `${year}년`, val: year.toString() }; }); // 현재 날짜를 기준으로 현재 분기까지의 분기 옵션 생성 export const getQuarterOptions = (selectedYear?: string): { text: string; val: string }[] => { const currentDate = new Date(); const currentYear = currentDate.getFullYear().toString(); const currentMonth = currentDate.getMonth() + 1; // 0부터 시작하므로 +1 // 현재 년도인 경우에만 현재 날짜까지의 분기 표시 if (selectedYear === currentYear) { // 현재 월을 기준으로 현재 분기 계산 const currentQuarter = Math.ceil(currentMonth / 3); // 현재 분기까지의 분기 옵션 생성 const quarterOptions = []; for (let i = 1; i <= currentQuarter; i++) { quarterOptions.push({ text: `${i}분기`, val: i.toString() }); } return quarterOptions; } else { // 이전 년도인 경우 모든 분기 표시 return [ { text: "1분기", val: "1" }, { text: "2분기", val: "2" }, { text: "3분기", val: "3" }, { text: "4분기", val: "4" } ]; } }; // ["202506", "202505", "202502", "202405"] 을 SelectBox의 옵션으로 변환 export const convertToTextValArray = (yyyymms: string[]): { text: string; val: string }[] => { return yyyymms.map(dateStr => { const year = dateStr.slice(0, 4); const month = dateStr.slice(4, 6); return { text: `${year}-${month}`, val: dateStr }; }); }; // 🎯 날짜 유효성 검사 함수 (8자리 또는 6자리 지원) export const isValidDateString = (dateStr: string): boolean => { // 숫자만 추출 const cleanStr = dateStr.replace(/\D/g, ''); // 8자리 (yyyymmdd) 또는 6자리 (yyyymm) 확인 if (cleanStr.length === 8) { // 연도, 월, 일 추출 const year = parseInt(cleanStr.substring(0, 4)); const month = parseInt(cleanStr.substring(4, 6)); const day = parseInt(cleanStr.substring(6, 8)); // 기본 범위 검사 if (year < 1900 || year > 2200) return false; if (month < 1 || month > 12) return false; if (day < 1 || day > 31) return false; // Date 객체로 실제 유효한 날짜인지 검사 const date = new Date(year, month - 1, day); return date.getFullYear() === year && date.getMonth() === month - 1 && date.getDate() === day; } else if (cleanStr.length === 6) { // 연도, 월 추출 const year = parseInt(cleanStr.substring(0, 4)); const month = parseInt(cleanStr.substring(4, 6)); // 기본 범위 검사 if (year < 1900 || year > 2200) return false; if (month < 1 || month > 12) return false; return true; } return false; }; // 🎯 날짜 포맷팅 함수 export const formatDateString = (dateStr: string): string => { const cleanStr = dateStr.replace(/\D/g, ''); // 숫자만 추출 if (!isValidDateString(cleanStr)) { return dateStr; // 유효하지 않은 날짜면 원본 반환 } const year = cleanStr.substring(0, 4); const month = cleanStr.substring(4, 6); // parseInt로 앞의 0 제거 // const month = parseInt(cleanStr.substring(4, 6)); // parseInt로 앞의 0 제거 // const day = parseInt(cleanStr.substring(6, 8)); // parseInt로 앞의 0 제거 const day = cleanStr.substring(6, 8); // parseInt로 앞의 0 제거 return `${year}-${month}-${day}`; };