feat(components): 添加时间戳和日期时间转换功能

- 实现了时间戳实时显示组件,支持毫秒/秒单位切换和自动更新
- 添加了日期时间转时间戳转换器,支持多种时区选择和单位转换
- 实现了时间戳转日期时间转换器,支持毫秒/秒单位和时区转换
- 为App.css添加了响应式样式和时间戳组件相关样式
- 为时间戳和转换组件添加了错误处理和输入验证功能
- 实现在index.css中重置标题样式以保持一致性
This commit is contained in:
雨霖铃
2026-01-07 21:42:59 +08:00
parent 0cad722479
commit 11e941d5fe
5 changed files with 828 additions and 109 deletions
+144 -27
View File
@@ -1,66 +1,183 @@
import {useMemo, useState} from "react";
import {useState, useCallback} from "react";
import {formatWithZone, TimezoneOptions} from "../utils/timeUtils";
/**
* 时间戳转日期时间组件
*
* 功能特性:
* 1. 将时间戳转换为日期时间字符串
* 2. 支持多种时区选择
* 3. 支持毫秒和秒单位切换
* 4. 提供输入验证和错误提示
*
* @component
* @example
* ```jsx
* <TimestampToDatetime />
* ```
*
* @returns {JSX.Element} 时间戳转日期时间组件
*/
// 常用时区列表
const TIME_ZONE_LIST = [
'America/New_York',
'America/Chicago',
'America/Denver',
'America/Los_Angeles',
'America/Anchorage',
'America/Honolulu',
'Europe/London',
'Europe/Paris',
'Europe/Berlin',
'Europe/Moscow',
'Asia/Tokyo',
'Asia/Shanghai',
'Asia/Hong_Kong',
'Asia/Singapore',
'Asia/Dubai',
'Asia/Kolkata',
'Australia/Sydney',
'Pacific/Auckland',
];
// 时间戳单位选项
const TIMESTAMP_UNITS = [
{value: 'milliseconds', label: '毫秒(ms)'},
{value: 'seconds', label: '秒(s)'},
];
export function TimestampToDatetime() {
/** @type {[string, function]} 输入的时间戳值 */
const [timestampValue, setTimestampValue] = useState(Date.now());
/** @type {[string, function]} 转换结果 */
const [timestampResult, setTimestampResult] = useState('');
/** @type {[string, function]} 时间戳单位 ('milliseconds' | 'seconds') */
const [unit, setUnit] = useState('milliseconds');
/** @type {[string, function]} 选择的时区 */
const [selectedZone, setSelectedZone] = useState('Asia/Shanghai');
const timeZoneList = useMemo(() => ['America/New_York', 'America/Chicago', 'America/Denver', 'America/Los_Angeles', 'America/Anchorage', 'America/Honolulu', 'Europe/London', 'Europe/Paris', 'Europe/Berlin', 'Europe/Moscow', 'Asia/Tokyo', 'Asia/Shanghai', 'Asia/Hong_Kong', 'Asia/Singapore', 'Asia/Dubai', 'Asia/Kolkata', 'Australia/Sydney', 'Pacific/Auckland',], []);
function handleConvertTimestampToDate() {
if (!timestampValue) {
setTimestampResult('请输入时间戳');
return;
/** @type {[string, function]} 错误信息 */
const [error, setError] = useState('');
/**
* 转换时间戳为日期时间
* @type {function(): void}
*/
const handleConvertTimestampToDate = useCallback(() => {
try {
setError('');
if (!timestampValue) {
setError('请输入时间戳');
setTimestampResult('');
return;
}
const numericValue = Number(timestampValue);
if (isNaN(numericValue)) {
setError('无效的时间戳格式');
setTimestampResult('');
return;
}
const result = formatWithZone(numericValue, selectedZone, unit);
setTimestampResult(result);
} catch (err) {
setError('转换失败,请检查输入格式');
setTimestampResult('');
}
const result = formatWithZone(timestampValue, selectedZone, unit);
setTimestampResult(result);
}
}, [timestampValue, selectedZone, unit]);
const handleInputChange = (e) => setTimestampValue(e.target.value);
/**
* 处理时间戳输入变化
* @type {function(React.ChangeEvent<HTMLInputElement>): void}
*/
const handleInputChange = useCallback((e) => {
setTimestampValue(e.target.value);
setError(''); // 清除错误信息
}, []);
/**
* 处理时区选择变化
* @type {function(React.ChangeEvent<HTMLSelectElement>): void}
*/
const handleZoneChange = useCallback((e) => {
setSelectedZone(e.target.value);
}, []);
/**
* 处理时间戳单位变化
* @type {function(React.ChangeEvent<HTMLSelectElement>): void}
*/
const handleUnitChange = useCallback((e) => {
setUnit(e.target.value);
}, []);
return (
<div>
<h2>时间戳转日期时间</h2>
<div>
<div style={{display: 'flex', gap: '8px', alignItems: 'center'}}>
<div className="datetime-converter">
<h2 className="converter-title">时间戳转日期时间</h2>
<div className="converter-form">
<div className="input-group">
<input
type="number"
placeholder="输入时间戳"
placeholder="输入时间戳 (如: 1704067200000)"
value={timestampValue}
className="datetime-input"
onChange={handleInputChange}
style={{minWidth: '200px'}}
aria-label="输入要转换的时间戳"
title="支持毫秒或秒为单位的时间戳"
/>
<select
value={unit}
aria-label='选择时间戳单位'
onChange={(e) => setUnit(e.target.value)}
className="unit-select"
onChange={handleUnitChange}
aria-label="选择时间戳单位"
>
<option value="milliseconds">毫秒(ms)</option>
<option value="seconds">(s)</option>
{TIMESTAMP_UNITS.map(({value, label}) => (
<option key={value} value={value}>
{label}
</option>
))}
</select>
</div>
<div style={{marginBottom: '20px'}}>
<button className={'action-btn'} onClick={handleConvertTimestampToDate}>
{error && (
<div className="error-message" role="alert">
{error}
</div>
)}
<div className="action-group">
<button
className="converter-btn action-btn"
onClick={handleConvertTimestampToDate}
aria-label="转换时间戳为日期时间"
>
转换
</button>
</div>
<div style={{display: 'flex', gap: '8px', alignItems: 'center'}}>
<div className="result-group">
<input
type="text"
placeholder="转换结果"
value={timestampResult}
className="result-input"
readOnly
style={{minWidth: '200px'}}
aria-label="转换结果"
/>
<select
value={selectedZone}
onChange={(e) => setSelectedZone(e.target.value)}
className="timezone-select"
onChange={handleZoneChange}
aria-label="选择时区"
>
<TimezoneOptions zones={timeZoneList}/>
<TimezoneOptions zones={TIME_ZONE_LIST}/>
</select>
</div>
</div>