import React, { useState, useMemo } from 'react'; import { Link } from 'react-router-dom'; import './DayCalc.css'; const today = () => new Date().toISOString().slice(0, 10); const fmt = (d) => { if (!d) return ''; const [y, m, day] = d.split('-'); return `${y}년 ${parseInt(m)}월 ${parseInt(day)}일`; }; // 두 날짜 사이 diff 계산 const calcDiff = (from, to) => { const f = new Date(from); const t = new Date(to); const totalMs = t - f; const totalDays = Math.round(totalMs / 86400000); // 연/월/일 분리 계산 let years = t.getFullYear() - f.getFullYear(); let months = t.getMonth() - f.getMonth(); let days = t.getDate() - f.getDate(); if (days < 0) { months -= 1; const prevMonth = new Date(t.getFullYear(), t.getMonth(), 0); days += prevMonth.getDate(); } if (months < 0) { years -= 1; months += 12; } const totalMonths = years * 12 + months; const weeks = Math.floor(Math.abs(totalDays) / 7); const remDays = Math.abs(totalDays) % 7; return { totalDays, totalMonths, years, months, days, weeks, remDays }; }; // 특정 날짜로부터 N일 후 날짜 계산 const addDays = (dateStr, n) => { const d = new Date(dateStr); d.setDate(d.getDate() + n); return d.toISOString().slice(0, 10); }; // 기념일 체크포인트 const MILESTONES = [100, 200, 365, 500, 730, 1000, 1461, 2000, 3000]; const QUICK_PRESETS = [ { label: '오늘 기준', offset: 0 }, { label: '1주 후', offset: 7 }, { label: '1개월 후', offset: 30 }, { label: '3개월 후', offset: 90 }, { label: '6개월 후', offset: 180 }, { label: '1년 후', offset: 365 }, ]; const DayCalc = () => { const [fromDate, setFromDate] = useState(''); const [toDate, setToDate] = useState(today()); const [tab, setTab] = useState('diff'); // diff | milestone | future const result = useMemo(() => { if (!fromDate || !toDate) return null; try { return calcDiff(fromDate, toDate); } catch { return null; } }, [fromDate, toDate]); const milestones = useMemo(() => { if (!fromDate) return []; return MILESTONES.map((n) => ({ days: n, date: addDays(fromDate, n - 1), })); }, [fromDate]); const isForward = result ? result.totalDays >= 0 : true; const applyPreset = (offset) => { setToDate(addDays(today(), offset)); }; return (
← Lab

Lab · 날짜 도구

일수 계산기

두 날짜 사이의 기간과 기념일 날짜를 계산합니다.

{/* 날짜 입력 */}
setFromDate(e.target.value)} max={toDate || undefined} /> {fromDate && {fmt(fromDate)}}
{result ? {isForward ? '→' : '←'} : }
setToDate(e.target.value)} /> {toDate && {fmt(toDate)}}
{/* 빠른 종료일 설정 */}
빠른 설정 {QUICK_PRESETS.map((p) => ( ))}
{/* 결과 탭 */} {fromDate && ( <>
{[ { id: 'diff', label: '기간 계산' }, { id: 'milestone', label: '기념일' }, ].map((t) => ( ))}
{/* 기간 계산 탭 */} {tab === 'diff' && result && (
{/* 메인 수치 */}

{isForward ? '+' : ''}{result.totalDays.toLocaleString()}

{isForward ? '경과' : '이전'}

{result.totalMonths.toLocaleString()}

개월

총 개월 수

{result.weeks.toLocaleString()}

주 {result.remDays}일

주 단위

{/* 세부 분해 */}

상세 기간

{result.years > 0 && (
{result.years}
)}
{result.months} 개월
{result.days}

{fmt(fromDate)} 부터 {fmt(toDate)} 까지

)} {/* 기념일 탭 */} {tab === 'milestone' && (

{fmt(fromDate)} 을 기준으로 한 기념일 날짜입니다.

{milestones.map(({ days, date }) => { const isPast = date < today(); const isToday = date === today(); const diff = calcDiff(today(), date); return (
{days < 365 ? `D+${days}` : days % 365 === 0 ? `${days / 365}주년` : `D+${days}`}
{fmt(date)}
{isToday ? '🎉 오늘' : isPast ? `${Math.abs(diff.totalDays)}일 전` : `D-${diff.totalDays}`}
); })}
)} )} {!fromDate && (

📅

시작일을 입력하면 기간 계산을 시작합니다.

)}
); }; export default DayCalc;