/
Kovalenko
/
TODO
Обзор
Документация
Войти
/
Kovalenko
/
TODO
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
frontend/src/utils/date.ts
61 строка
2 KB
Kovalenko1
Done
29 дек 2025, 11:50
29 дек 2025, 11:50
f453b3b
Код
Авторство
О чём код?
import type { RepeatMode } from '../types/ui'; export function normalizeDateTime(value: string | null | undefined) { if (!value) return null; const date = new Date(value); return Number.isNaN(date.getTime()) ? null : date.toISOString(); } export function toDateKey(value: string | Date) { const date = value instanceof Date ? value : new Date(value); if (Number.isNaN(date.getTime())) return ''; const y = date.getFullYear(); const m = String(date.getMonth() + 1).padStart(2, '0'); const d = String(date.getDate()).padStart(2, '0'); return `${y}-${m}-${d}`; } export function toInputValue(value: string | null | undefined) { if (!value) return ''; const date = new Date(value); if (Number.isNaN(date.getTime())) return ''; const offsetDate = new Date(date.getTime() - date.getTimezoneOffset() * 60000); return offsetDate.toISOString().slice(0, 16); } export function buildOccurrences(base: string, mode: RepeatMode, count: number) { if (mode === 'none') return [normalizeDateTime(base)]; const normalizedCount = Math.max(1, Math.min(count || 1, 12)); const start = new Date(base); if (Number.isNaN(start.getTime())) return [null]; return Array.from({ length: normalizedCount }).map((_, index) => { const next = new Date(start); if (mode === 'daily') { next.setDate(start.getDate() + index); } else if (mode === 'weekly') { next.setDate(start.getDate() + index * 7); } return next.toISOString(); }); } export function formatDate(value: string | null) { if (!value) return 'н/д'; const date = new Date(value); return date.toLocaleString('ru-RU', { dateStyle: 'medium', timeStyle: 'short' }); } export function formatTime(value: string | null) { if (!value) return 'н/д'; const date = new Date(value); return date.toLocaleTimeString('ru-RU', { hour: '2-digit', minute: '2-digit' }); } export function formatDayName(date: Date) { return date.toLocaleDateString('ru-RU', { weekday: 'short' }); } export function formatDayNumber(date: Date) { return date.toLocaleDateString('ru-RU', { month: 'short', day: 'numeric' }); }