关于 1k-date-formatting
面向 OneKey 贡献者的编码技能,通过共享工具、选项和 React Hook 统一日期与时间格式。
主要功能
- 01共享日期与时间工具
- 02相对日期和时间距离格式化
- 03自定义格式模板与选项
- 04用于动态格式化的 React Hook
- 05本地化格式指南
适用场景
- 01格式化交易历史日期
- 02显示相对时间戳
- 03应用自定义日期格式
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ onekeyhq/ app-monorepo --skill 1k-date-formatting安装后在 Claude Code 中输入/1k-date-formatting确认已加载。
附带文件references/rules/date-formatting.md
详细介绍
日期格式化
OneKey 应用中日期和时间格式化的一致性指南。
重要规则
切勿使用 JavaScript 原生日期方法:
// ❌ FORBIDDEN
date.toLocaleDateString()
date.toLocaleString()
date.toISOString()
new Intl.DateTimeFormat().format(date)
// ✅ CORRECT
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?) | 自定义格式 | 基于模板 |
常见模式
交易历史
import { formatDate } from '@onekeyhq/shared/src/utils/dateUtils';
// Hide year if current year, hide seconds
<SizableText>
{formatDate(item.createdAt, { hideTheYear: true, hideSeconds: true })}
</SizableText>
自定义格式
// Use format template
{formatDate(item.timestamp, { formatTemplate: 'yyyy-LL-dd HH:mm' })}
React Hook(用于动态更新)
import useFormatDate from '@onekeyhq/kit/src/hooks/useFormatDate';
function MyComponent() {
const { formatDate } = useFormatDate();
return <SizableText>{formatDate(date, { hideSeconds: true })}</SizableText>;
}
格式选项
interface IFormatDateOptions {
hideYear?: boolean; // Always hide year
hideMonth?: boolean; // Always hide month
hideTheYear?: boolean; // Hide year if current year
hideTheMonth?: boolean; // Hide month if current month
hideTimeForever?: boolean; // Hide time portion
hideSeconds?: boolean; // Hide seconds (HH:mm)
formatTemplate?: string; // Custom date-fns format
}
详细指南
如需了解全面的日期格式化规则和示例,请参阅 date-formatting.md。
涵盖主题:
@onekeyhq/shared/src/utils/dateUtils中的核心工具- 可用的格式化函数
- 选项参考和格式模板
- 交易、历史记录和相对时间的常见模式
- 用于动态更新的 React Hook
- 区域设置感知的格式化
- 实际示例
- 故障排除
关键文件
| 用途 | 文件路径 |
|---|---|
| 核心工具 | 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- 通用编码模式
评论
支持 Markdown