import React, { useState, useEffect } from 'react'; interface CountdownProps { targetDate: string; // 目标日期,格式:'YYYY-MM-DD' } export const Countdown: React.FC = ({ targetDate }) => { const [timeLeft, setTimeLeft] = useState({ days: 0, hours: 0, minutes: 0, seconds: 0 }); useEffect(() => { const timer = setInterval(() => { const now = new Date().getTime(); const target = new Date(targetDate).getTime(); const difference = target - now; if (difference > 0) { const days = Math.floor(difference / (1000 * 60 * 60 * 24)); const hours = Math.floor((difference % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)); const minutes = Math.floor((difference % (1000 * 60 * 60)) / (1000 * 60)); const seconds = Math.floor((difference % (1000 * 60)) / 1000); setTimeLeft({ days, hours, minutes, seconds }); } }, 1000); return () => clearInterval(timer); }, [targetDate]); const TimeBox = ({ value, label }: { value: number; label: string }) => (
{value.toString().padStart(2, '0')}
{label}
); return (
); };