/** * Timezone utilities - uses client device timezone */ /** * Get the browser/device timezone * Returns IANA timezone string like 'Asia/Bishkek' or 'Europe/Moscow' */ export function getClientTimezone(): string { try { return Intl.DateTimeFormat().resolvedOptions().timeZone } catch { return 'UTC' } } /** * Get timezone offset in hours from UTC * Positive for east of UTC (e.g., +6 for Asia/Bishkek) */ export function getTimezoneOffset(): number { return -new Date().getTimezoneOffset() / 60 } /** * Format date in client's local timezone */ export function formatLocalDate(date: Date | string, formatStr: string = 'dd.MM.yyyy'): string { const d = typeof date === 'string' ? new Date(date) : date const options: Intl.DateTimeFormatOptions = {} if (formatStr.includes('dd') || formatStr.includes('d')) { options.day = formatStr.includes('dd') ? '2-digit' : 'numeric' } if (formatStr.includes('MM') || formatStr.includes('M')) { options.month = formatStr.includes('MMMM') ? 'long' : formatStr.includes('MMM') ? 'short' : '2-digit' } if (formatStr.includes('yyyy') || formatStr.includes('y')) { options.year = 'numeric' } if (formatStr.includes('HH') || formatStr.includes('H')) { options.hour = '2-digit' options.hour12 = false } if (formatStr.includes('mm') || formatStr.includes('m')) { options.minute = '2-digit' } if (formatStr.includes('EEEE') || formatStr.includes('EEE')) { options.weekday = formatStr.includes('EEEE') ? 'long' : 'short' } return d.toLocaleString('ru-RU', options) } /** * Format time in client's local timezone */ export function formatLocalTime(date: Date | string): string { const d = typeof date === 'string' ? new Date(date) : date return d.toLocaleTimeString('ru-RU', { hour: '2-digit', minute: '2-digit', hour12: false }) } /** * Format full datetime in client's local timezone */ export function formatLocalDateTime(date: Date | string): string { const d = typeof date === 'string' ? new Date(date) : date return d.toLocaleString('ru-RU', { day: '2-digit', month: 'long', year: 'numeric', hour: '2-digit', minute: '2-digit', hour12: false }) } /** * Convert local date/time to ISO string for API * This preserves the local time when sent to server */ export function localToISOString(date: Date): string { return date.toISOString() } /** * Parse ISO string from API to local Date */ export function isoToLocalDate(isoString: string): Date { return new Date(isoString) } /** * Get current date/time in client timezone */ export function nowLocal(): Date { return new Date() } /** * Check if date is today in client timezone */ export function isToday(date: Date | string): boolean { const d = typeof date === 'string' ? new Date(date) : date const today = new Date() return d.toDateString() === today.toDateString() } /** * Get timezone display name */ export function getTimezoneDisplayName(): string { const tz = getClientTimezone() const offset = getTimezoneOffset() const sign = offset >= 0 ? '+' : '' return `${tz} (UTC${sign}${offset})` }