// Helpers for parsing backend datetimes and computing week grids.
// Backend stores start_time / end_time as strings like "2026-08-10 19:00"
// (sometimes ISO "2026-08-10T19:00"). Parse both robustly as local time.
export function parseDateTime(value: string): Date {
if (!value) return new Date(NaN)
const normalized = value.trim().replace(" ", "T")
const d = new Date(normalized)
return d
}
export function startOfWeek(date: Date): Date {
const d = new Date(date)
d.setHours(0, 0, 0, 0)
const day = d.getDay() // 0 = Sunday
d.setDate(d.getDate() - day)
return d
}
export function addDays(date: Date, days: number): Date {
const d = new Date(date)
d.setDate(d.getDate() + days)
return d
}
export function isSameDay(a: Date, b: Date): boolean {
return (
a.getFullYear() === b.getFullYear() &&
a.getMonth() === b.getMonth() &&
a.getDate() === b.getDate()
)
}
export function toDateParam(date: Date): string {
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}`
}
// Format a Date into the "YYYY-MM-DD HH:MM" string the backend expects.
export function toBackendDateTime(date: Date): string {
const y = date.getFullYear()
const mo = String(date.getMonth() + 1).padStart(2, "0")
const d = String(date.getDate()).padStart(2, "0")
const h = String(date.getHours()).padStart(2, "0")
const mi = String(date.getMinutes()).padStart(2, "0")
return `${y}-${mo}-${d} ${h}:${mi}`
}
// For values ("YYYY-MM-DDTHH:MM").
export function toInputDateTime(date: Date): string {
return toBackendDateTime(date).replace(" ", "T")
}
// For values ("HH:MM").
export function toInputTime(date: Date): string {
const h = String(date.getHours()).padStart(2, "0")
const mi = String(date.getMinutes()).padStart(2, "0")
return `${h}:${mi}`
}
export function formatTime(date: Date): string {
if (isNaN(date.getTime())) return "--:--"
return date.toLocaleTimeString([], { hour: "2-digit", minute: "2-digit", hour12: false })
}
export function formatDayLabel(date: Date): string {
return date.toLocaleDateString([], { weekday: "long", month: "long", day: "numeric" })
}
export function formatMonthYear(date: Date): string {
return date.toLocaleDateString([], { month: "long", year: "numeric" })
}
export const WEEKDAY_LABELS = ["SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"]