Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01BJKMLHdxMwSUeYcdkwqX4W
18 lines
1023 B
TypeScript
18 lines
1023 B
TypeScript
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;
|
||
}
|