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 · 날짜 도구
두 날짜 사이의 기간과 기념일 날짜를 계산합니다.
{isForward ? '+' : ''}{result.totalDays.toLocaleString()}
일
{isForward ? '경과' : '이전'}
{result.totalMonths.toLocaleString()}
개월
총 개월 수
{result.weeks.toLocaleString()}
주 {result.remDays}일
주 단위
{fmt(fromDate)} 부터 {fmt(toDate)} 까지
{fmt(fromDate)} 을 기준으로 한 기념일 날짜입니다.
📅
시작일을 입력하면 기간 계산을 시작합니다.