Files
deerflow-code/frontend-web/src/core/utils/datetime.ts
T
吉鑫升andClaude Opus 4.8 74113b3e6e feat: 智能体与技能列表显示创建时间字段
- 新增共享 formatDateTime 工具(YYYY-MM-DD HH:mm)
- agent-card 卡片/列表视图在发布者下方显示创建时间
- skill 设置页 metaNodes 显示创建时间

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-12 21:20:33 +08:00

43 lines
1.4 KiB
TypeScript

import { formatDistanceToNow } from "date-fns";
import { enUS as dateFnsEnUS, zhCN as dateFnsZhCN } from "date-fns/locale";
import { detectLocale, type Locale } from "@/core/i18n";
import { getLocaleFromCookie } from "@/core/i18n/cookies";
function getDateFnsLocale(locale: Locale) {
switch (locale) {
case "zh-CN":
return dateFnsZhCN;
case "en-US":
default:
return dateFnsEnUS;
}
}
export function formatTimeAgo(date: Date | string | number, locale?: Locale) {
const effectiveLocale =
locale ??
(getLocaleFromCookie() as Locale | null) ??
// Fallback when cookie is missing (or on first render)
detectLocale();
return formatDistanceToNow(date, {
addSuffix: true,
locale: getDateFnsLocale(effectiveLocale),
});
}
/**
* Format a created/updated timestamp as `YYYY-MM-DD HH:mm`.
* Returns an empty string for missing or unparseable values.
*/
export function formatDateTime(value?: Date | string | number | null): string {
if (value === null || value === undefined || value === "") return "";
const date = value instanceof Date ? value : new Date(value);
if (Number.isNaN(date.getTime())) return "";
const pad = (n: number) => String(n).padStart(2, "0");
return (
`${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} ` +
`${pad(date.getHours())}:${pad(date.getMinutes())}`
);
}