"use client"; import { useState, useEffect } from "react"; import { Clock, ArrowLeftRight, Copy, RefreshCw } from "lucide-react"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { Tabs, TabsList, TabsTrigger } from "@/components/ui/tabs"; import { toast } from "sonner"; import dayjs from "dayjs"; export default function TimestampPage() { const [timestamp, setTimestamp] = useState(""); const [dateTime, setDateTime] = useState(""); const [unit, setUnit] = useState<"seconds" | "milliseconds">("seconds"); const [currentTimestamp, setCurrentTimestamp] = useState(""); useEffect(() => { // Initialize date time with current time setDateTime(dayjs().format("YYYY-MM-DDTHH:mm:ss")); }, []); useEffect(() => { const updateCurrent = () => { const now = Date.now(); setCurrentTimestamp( unit === "seconds" ? Math.floor(now / 1000).toString() : now.toString() ); }; updateCurrent(); const timer = setInterval(updateCurrent, 1000); return () => clearInterval(timer); }, [unit]); const handleUnitChange = (newUnit: "seconds" | "milliseconds") => { if (timestamp && !isNaN(Number(timestamp))) { const ts = Number(timestamp); if (unit === "seconds" && newUnit === "milliseconds") { setTimestamp((ts * 1000).toString()); } else if (unit === "milliseconds" && newUnit === "seconds") { setTimestamp(Math.floor(ts / 1000).toString()); } } setUnit(newUnit); }; const timestampToDate = () => { if (!timestamp) { toast.warning("请输入时间戳"); return; } const ts = parseInt(timestamp); if (isNaN(ts)) { toast.error("无效的时间戳"); return; } const date = unit === "seconds" ? dayjs.unix(ts) : dayjs(ts); if (!date.isValid()) { toast.error("无效的时间戳"); return; } setDateTime(date.format("YYYY-MM-DDTHH:mm:ss")); toast.success("转换成功"); }; const dateToTimestamp = () => { if (!dateTime) { toast.warning("请选择日期时间"); return; } const date = dayjs(dateTime); const ts = unit === "seconds" ? date.unix() : date.valueOf(); setTimestamp(ts.toString()); toast.success("转换成功"); }; const copyToClipboard = async (text: string) => { try { await navigator.clipboard.writeText(text); toast.success("已复制到剪贴板"); } catch { toast.error("复制失败"); } }; const setNow = () => { const now = dayjs(); setDateTime(now.format("YYYY-MM-DDTHH:mm:ss")); const ts = unit === "seconds" ? now.unix() : now.valueOf(); setTimestamp(ts.toString()); }; return (
{/* Page Header */}

时间戳转换

Unix 时间戳与日期时间互相转换

{/* Current Timestamp */} 当前时间戳 handleUnitChange(v as "seconds" | "milliseconds")} className="w-45" > 秒(s) 毫秒(ms)
{currentTimestamp}
{/* Timestamp to Date */} 时间戳 → 日期时间
setTimestamp(e.target.value)} className="pr-20 font-mono" />
{/* Date to Timestamp */} 日期时间 → 时间戳
setDateTime(e.target.value)} className="font-mono" />
{/* Result Display */} 转换结果

时间戳 ({unit === "seconds" ? "秒" : "毫秒"})

{timestamp || "-"}

日期时间

{dateTime ? dayjs(dateTime).format("YYYY-MM-DD HH:mm:ss") : "-"}
{/* Info Card */} 💡 使用说明
  • Unix 时间戳是从 1970-01-01 00:00:00 UTC 开始的秒数
  • 支持秒级和毫秒级时间戳转换
  • JavaScript 通常使用毫秒级时间戳
  • 后端语言(如 PHP、Python)通常使用秒级时间戳
  • 点击"使用当前"可快速填入当前时间戳
  • 支持复制当前实时时间戳
); }