| |
| export function formatDateTime(epochSeconds: number, locale = 'en', tz = 'Asia/Riyadh'): string { |
| try { |
| return new Intl.DateTimeFormat(locale === 'ar' ? 'ar-SA' : 'en-GB', { |
| dateStyle: 'medium', timeStyle: 'short', timeZone: tz, |
| }).format(new Date(epochSeconds * 1000)) |
| } catch { |
| return new Date(epochSeconds * 1000).toISOString() |
| } |
| } |
|
|
| |
| |
| const MIN_VALID_MS = Date.UTC(2000, 0, 1) |
|
|
| |
| |
| |
| |
| |
| export function formatDateTimeSafe( |
| value: number | string | null | undefined, |
| locale = 'en', |
| tz = 'Asia/Riyadh', |
| ): string | null { |
| if (value === null || value === undefined) return null |
| let ms: number |
| if (typeof value === 'number') { |
| if (!Number.isFinite(value)) return null |
| ms = value * 1000 |
| } else { |
| const s = value.trim() |
| if (s === '') return null |
| ms = new Date(s).getTime() |
| } |
| if (!Number.isFinite(ms) || ms < MIN_VALID_MS) return null |
| try { |
| return new Intl.DateTimeFormat(locale === 'ar' ? 'ar-SA' : 'en-GB', { |
| dateStyle: 'medium', timeStyle: 'short', timeZone: tz, |
| }).format(new Date(ms)) |
| } catch { |
| return null |
| } |
| } |
|
|