Files
ftb-project-management/apps/web/lib/format.ts
2026-06-26 15:50:30 +08:00

57 lines
1.8 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 时间格式化工具——统一处理 ISO 时间戳到本地时区显示
* 避免在 UI 直接 slice ISO 字符串导致显示 UTC 时间
*/
/**
* 格式化为 YYYY-MM-DD HH:mm本地时区
*/
export function formatDateTime(iso?: string | null): string {
if (!iso) return '-';
const d = new Date(iso);
if (isNaN(d.getTime())) return '-';
const yyyy = d.getFullYear();
const mm = String(d.getMonth() + 1).padStart(2, '0');
const dd = String(d.getDate()).padStart(2, '0');
const hh = String(d.getHours()).padStart(2, '0');
const mi = String(d.getMinutes()).padStart(2, '0');
return `${yyyy}-${mm}-${dd} ${hh}:${mi}`;
}
/**
* 格式化为 YYYY-MM-DD本地时区
*/
export function formatDate(iso?: string | null): string {
if (!iso) return '-';
const d = new Date(iso);
if (isNaN(d.getTime())) return '-';
const yyyy = d.getFullYear();
const mm = String(d.getMonth() + 1).padStart(2, '0');
const dd = String(d.getDate()).padStart(2, '0');
return `${yyyy}-${mm}-${dd}`;
}
/**
* 格式化为 YYYY-MM-DD本地日期用于 date input / 今日筛选)
*/
export function formatLocalDate(date: Date = new Date()): string {
const yyyy = date.getFullYear();
const mm = String(date.getMonth() + 1).padStart(2, '0');
const dd = String(date.getDate()).padStart(2, '0');
return `${yyyy}-${mm}-${dd}`;
}
/**
* 格式化为 MM-DD HH:mm紧凑版本地时区
*/
export function formatDateTimeShort(iso?: string | null): string {
if (!iso) return '-';
const d = new Date(iso);
if (isNaN(d.getTime())) return '-';
const mm = String(d.getMonth() + 1).padStart(2, '0');
const dd = String(d.getDate()).padStart(2, '0');
const hh = String(d.getHours()).padStart(2, '0');
const mi = String(d.getMinutes()).padStart(2, '0');
return `${mm}-${dd} ${hh}:${mi}`;
}