34c7d2d65c
Ersetzt natives type=time (AM/PM je nach System) durch Stunde/Minute-Auswahl, wandelt 12h-Werte beim Laden um und stellt html lang auf de/en zurück. Co-authored-by: Cursor <cursoragent@cursor.com>
38 lines
1.1 KiB
TypeScript
38 lines
1.1 KiB
TypeScript
/** BCP 47 locales that use 24-hour clock for Intl formatting. */
|
|
export function resolveIntlLocale(language?: string): string {
|
|
const lng = (language ?? 'en').toLowerCase()
|
|
return lng.startsWith('de') ? 'de-DE' : 'en-GB'
|
|
}
|
|
|
|
const APP_DATE_TIME_OPTIONS: Intl.DateTimeFormatOptions = {
|
|
year: 'numeric',
|
|
month: '2-digit',
|
|
day: '2-digit',
|
|
hour: '2-digit',
|
|
minute: '2-digit',
|
|
hour12: false
|
|
}
|
|
|
|
const APP_TIME_OPTIONS: Intl.DateTimeFormatOptions = {
|
|
hour: '2-digit',
|
|
minute: '2-digit',
|
|
hour12: false
|
|
}
|
|
|
|
function toDate(value: Date | string | number): Date | null {
|
|
const date = value instanceof Date ? value : new Date(value)
|
|
return Number.isNaN(date.getTime()) ? null : date
|
|
}
|
|
|
|
export function formatAppDateTime(value: Date | string | number, language?: string): string {
|
|
const date = toDate(value)
|
|
if (!date) return String(value)
|
|
return date.toLocaleString(resolveIntlLocale(language), APP_DATE_TIME_OPTIONS)
|
|
}
|
|
|
|
export function formatAppTime(value: Date | string | number, language?: string): string {
|
|
const date = toDate(value)
|
|
if (!date) return String(value)
|
|
return date.toLocaleTimeString(resolveIntlLocale(language), APP_TIME_OPTIONS)
|
|
}
|