Skip to content

Commit

Permalink
Use the browsers language val, and tighten up the error checking.
Browse files Browse the repository at this point in the history
  • Loading branch information
MelissaAutumn committed Feb 12, 2025
1 parent 9decff9 commit 880fb57
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions frontend/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,17 @@ export const download = (data: BlobPart, filename: string, contenttype: string =
export const timeFormat = (): string => {
const fallbackFormat = import.meta.env?.VITE_DEFAULT_HOUR_FORMAT ?? 12;
const user = JSON.parse(localStorage?.getItem('tba/user') ?? '{}') as User;
const use12HourTime = Intl.DateTimeFormat().resolvedOptions()?.hour12;

let use12HourTime = null;
try {
use12HourTime = Intl.DateTimeFormat(window.navigator.language, { hour: 'numeric' }).resolvedOptions()?.hour12 ?? null;
} catch (e: RangeError|any) {
// Catch any range error raised by invalid language/locale codes and pass
}

// `.hour12` is an optional value and can be undefined. So default to our env value, and if not undefined use it.
let detected = fallbackFormat;
if (typeof use12HourTime !== 'undefined') {
if (use12HourTime !== null) {
detected = use12HourTime ? 12 : 24;
}

Expand Down

0 comments on commit 880fb57

Please sign in to comment.