import { Layout } from "@/components/Layout"; import { useState } from "react"; import { Input } from "@/components/ui/input"; import { Button } from "@/components/ui/button"; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"; import { Label } from "@/components/ui/label"; import { formatDateTime } from "@/components/SearchResults"; export default function Home() { const now = new Date(); const [time1Input, setTime1Input] = useState(formatDateTime(now)); const [time2Input, setTime2Input] = useState(formatDateTime(now)); const time1 = new Date(time1Input); const time2 = new Date(time2Input); const difference = time2.getTime() - time1.getTime(); const days = Math.floor(difference / (1000 * 60 * 60 * 24)); const hours = Math.floor(difference / (1000 * 60 * 60)) % 24; const minutes = Math.floor((difference / (1000 * 60)) % 60); const seconds = Math.floor((difference / 1000) % 60); const diffString = `${Math.abs(days) || 0} 天 ${Math.abs(hours) || 0} 时 ${Math.abs(minutes) || 0} 分`; const isNegative = difference < 0; const setQuickTime = (hoursOffset: number) => { const newTime = new Date(time1Input); newTime.setHours(newTime.getHours() + hoursOffset); setTime2Input(formatDateTime(newTime)); }; return (

时间计算器

输入两个时间点,计算精确的时间差

{/* 时间输入区域 */} 时间设置 选择或输入开始时间和结束时间
setTime1Input(e.target.value)} />
setTime2Input(e.target.value)} />
计算结果 两个时间点之间的精确时间差
{isNegative ? "时间差为负值" : diffString}
{isNegative && (
结束时间早于开始时间
)}
{Math.abs(days)}
天数
{Math.abs(hours)}
小时
{Math.abs(minutes)}
分钟
{Math.abs(seconds)}
秒数
); }