sdlc-hub/web/src/ui/format.ts
2026-10-28 12:48:40 +04:00

28 lines
1.5 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.

// форматтеры дат ядра: общие для оболочки и модулей
const pad = (n: number): string => String(n).padStart(2, "0");
// «ДД.ММ ЧЧ:ММ» в локальном времени; не дата — как есть, null — «—»
export function fmtStamp(iso: string | null): string {
if (iso === null) return "—";
const d = new Date(iso);
return isNaN(d.getTime()) ? String(iso) : `${pad(d.getDate())}.${pad(d.getMonth() + 1)} ${pad(d.getHours())}:${pad(d.getMinutes())}`;
}
// «ДД.ММ» для текущего года, «ДД.ММ.ГГГГ» для другого; null — «—»; не ГГГГ-ММ-ДД — как есть
export function fmtDate(iso: string | null, now: Date = new Date()): string {
if (iso === null) return "—";
const m = /^(\d{4})-(\d{2})-(\d{2})/.exec(iso);
if (!m) return iso;
return Number(m[1]) === now.getFullYear() ? `${m[3]}.${m[2]}` : `${m[3]}.${m[2]}.${m[1]}`;
}
// «ЧЧ:ММ» для событий текущих суток, иначе «ДД.ММ ЧЧ:ММ»; без времени — «—»
export function eventTime(at: string | null, now: Date, alwaysDate = false): string {
const d = at ? new Date(at) : null;
if (!d || isNaN(d.getTime())) return "—";
const hm = `${pad(d.getHours())}:${pad(d.getMinutes())}`;
const sameDay = d.getFullYear() === now.getFullYear() && d.getMonth() === now.getMonth() && d.getDate() === now.getDate();
return sameDay && !alwaysDate ? hm : `${pad(d.getDate())}.${pad(d.getMonth() + 1)} ${hm}`;
}