arm-web/src/utils/dates.ts
2026-10-18 13:31:51 +04:00

18 lines
1023 B
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 MOSCOW = new Intl.DateTimeFormat('ru-RU', { timeZone: 'Europe/Moscow', day: '2-digit', month: '2-digit', year: 'numeric' });
/** Момент времени из API (ISO, UTC) → «dd.mm.yyyy» по Москве (как считает даты бэкенд); пусто/мусор → null. */
export function formatMoscowDate(iso: string | null | undefined): string | null {
if (!iso) return null;
const date = new Date(iso);
return Number.isNaN(date.getTime()) ? null : MOSCOW.format(date);
}
/** Строго «ГГГГ-ММ-ДД» и существующая календарная дата (30 февраля — нет). */
export function isIsoDate(text: string): boolean {
const match = /^(\d{4})-(\d{2})-(\d{2})$/.exec(text);
if (!match) return false;
const [year, month, day] = [Number(match[1]), Number(match[2]), Number(match[3])];
const date = new Date(Date.UTC(year, month - 1, day));
return date.getUTCFullYear() === year && date.getUTCMonth() === month - 1 && date.getUTCDate() === day;
}