From a6d85962b75f800a1f50fe75c0d743566a7cc123 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9B=A8=E9=9C=96=E9=93=83?= Date: Mon, 2 Feb 2026 20:58:56 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BD=BF=E7=94=A8=20dayjs=20=E6=9B=BF?= =?UTF-8?q?=E6=8D=A2=E6=97=B6=E9=97=B4=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91?= =?UTF-8?q?=EF=BC=8C=E4=BC=98=E5=8C=96=E6=97=A5=E6=9C=9F=E6=97=B6=E9=97=B4?= =?UTF-8?q?=E8=BD=AC=E6=8D=A2=E5=8A=9F=E8=83=BD=EF=BC=8C=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E4=B8=8D=E5=86=8D=E4=BD=BF=E7=94=A8=E7=9A=84=E6=97=B6=E9=97=B4?= =?UTF-8?q?=E5=B7=A5=E5=85=B7=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../popup/components/DatetimeToTimestamp.tsx | 89 +++++++++--------- .../popup/components/TimestampToDatetime.tsx | 92 +++++++++---------- package-lock.json | 7 ++ package.json | 1 + tsconfig.json | 2 +- utils/dayjs.ts | 8 ++ utils/timeUtils.tsx | 58 ------------ 7 files changed, 100 insertions(+), 157 deletions(-) create mode 100644 utils/dayjs.ts delete mode 100644 utils/timeUtils.tsx diff --git a/entrypoints/popup/components/DatetimeToTimestamp.tsx b/entrypoints/popup/components/DatetimeToTimestamp.tsx index 885e4ec..52885c0 100644 --- a/entrypoints/popup/components/DatetimeToTimestamp.tsx +++ b/entrypoints/popup/components/DatetimeToTimestamp.tsx @@ -1,5 +1,5 @@ -import { formatWithDate, formatWithZone } from '../../../utils/timeUtils'; import { useState, useCallback } from 'react'; +import dayjs from '@/utils/dayjs'; /** * 日期时间转时间戳组件 @@ -49,7 +49,7 @@ const TIMESTAMP_UNITS = [ ]; export function DatetimeToTimestamp() { - const [dateValue, setDateValue] = useState(() => formatWithZone(Date.now(), 'Asia/Shanghai')); + const [dateValue, setDateValue] = useState(() => dayjs().format('YYYY/MM/DD HH:mm:ss')); const [selectedZone, setSelectedZone] = useState('Asia/Shanghai'); @@ -59,64 +59,54 @@ export function DatetimeToTimestamp() { const [error, setError] = useState(''); - /** - * 转换日期时间为时间戳 - */ - const handleConvertDatetimeToTimestamp = useCallback(() => { - try { - setError(''); - const timestamp = formatWithDate(dateValue, selectedZone); + const performConversion = useCallback( + (currentDate: string, zone: string, currentUnit: string) => { + try { + if (!currentDate) { + setError('请输入有效的日期时间'); + return ''; + } - if (typeof timestamp !== 'number' || isNaN(timestamp)) { - setError('无效的日期时间格式'); - setResult(''); - return; + const timestamp = dayjs.tz(currentDate, zone); + + if (!timestamp.isValid()) { + setError('无效的日期时间格式'); + return ''; + } + + setError(''); + + const ms = timestamp.valueOf(); + return currentUnit === TIMESTAMP_UNITS[0].value + ? ms.toString() + : Math.floor(ms / 1000).toString(); + } catch (err) { + console.error('转换错误:', err); + return ''; } + }, + [], + ); - const finalResult = unit === 'milliseconds' ? timestamp : Math.floor(timestamp / 1000); - - setResult(finalResult.toString()); - } catch (err) { - console.error('转换错误:', err); - setError('转换失败,请检查输入格式'); - setResult(''); - } - }, [dateValue, selectedZone, unit]); - - /** - * 处理日期时间输入变化 - */ - const handleDateChange = useCallback((e) => { - setDateValue(e.target.value); - setError(''); // 清除错误信息 - }, []); - - /** - * 处理时区选择变化 - */ - const handleZoneChange = useCallback((e) => { - setSelectedZone(e.target.value); - }, []); + const handleConvert = useCallback(() => { + const newResult = performConversion(dateValue, selectedZone, unit); + setResult(newResult); + }, [dateValue, selectedZone, unit, performConversion]); /** * 处理时间戳单位变化 */ const handleUnitChange = useCallback( - (e) => { + (e: React.ChangeEvent) => { const newUnit = e.target.value; setUnit(newUnit); // 如果已有结果,重新计算 if (result) { - const currentResult = parseInt(result, 10); - if (!isNaN(currentResult)) { - const newResult = - newUnit === 'milliseconds' ? currentResult * 1000 : Math.floor(currentResult / 1000); - setResult(newResult.toString()); - } + setResult(performConversion(dateValue, selectedZone, newUnit) || ''); } }, - [result], + [dateValue, selectedZone, result, performConversion], ); return ( @@ -130,14 +120,17 @@ export function DatetimeToTimestamp() { placeholder="输入日期时间 (如: 2024-01-01 12:00:00)" value={dateValue} className="datetime-input" - onChange={handleDateChange} + onChange={(e) => { + setDateValue(e.target.value); + if (error) setError(''); + }} aria-label="输入要转换的日期时间" title="支持格式: YYYY-MM-DD HH:mm:ss" />