SuperAPI AI 工具集

1k-date-formatting

前端与建站

查看项目

更新于 10-16 08:35

技能介绍

用途:为 OneKey 应用提供统一的日期与时间格式化规范,适用于在 UI 组件中显示日期、时间戳等场景。

核心规则:禁止使用原生 JavaScript 日期方法,包括 date.toLocaleDateString()、date.toLocaleString()、date.toISOString() 以及 new Intl.DateTimeFormat().format(date)。应改用:import { formatDate } from '@onekeyhq/shared/src/utils/dateUtils'; 然后调用 formatDate(date, { hideSeconds: true })。

主要函数与用途:formatDate(date, options?) 输出完整日期时间,如 2024/01/15, 14:30;formatTime(date, options?) 仅输出时间,如 14:30:45;formatRelativeDate(date) 输出相对日期,如 Today、Yesterday;formatDistanceToNow(date) 输出时间距离,如 2 hours ago;formatDateFns(date, format?) 按模板自定义格式。

常用选项(IFormatDateOptions):hideYear 始终隐藏年;hideMonth 始终隐藏月;hideTheYear 当年份为当前年时隐藏;hideTheMonth 当月份为当前月时隐藏;hideTimeForever 隐藏时间部分;hideSeconds 隐藏秒;formatTemplate 自定义 date-fns 格式模板,如 'yyyy-LL-dd HH:mm'。

React 用法:可使用 import useFormatDate from '@onekeyhq/kit/src/hooks/useFormatDate'; 在组件内 const { formatDate } = useFormatDate(); 以支持动态更新。

关键文件:核心工具位于 packages/shared/src/utils/dateUtils.ts,React Hook 位于 packages/kit/src/hooks/useFormatDate.ts,本地化映射位于 packages/shared/src/locale/dateLocaleMap.ts。相关技能包括 /1k-i18n 与 /1k-coding-patterns。更详细的规则与示例见 date-formatting.md。

限制:资料仅涉及 OneKey 内部规范与上述函数,具体错误处理、时区策略、各平台差异及完整模板列表暂未提供。