Files
antigol-service/frontend/composables/useBrowserDateTime.ts
talorr cda36918e8 init
2026-03-27 03:36:08 +03:00

35 lines
935 B
TypeScript

type BrowserDateTimeOptions = Intl.DateTimeFormatOptions;
const DEFAULT_DATE_TIME_OPTIONS: BrowserDateTimeOptions = {
day: "2-digit",
month: "2-digit",
year: "numeric",
hour: "2-digit",
minute: "2-digit"
};
export function useBrowserDateTime() {
const browserTimeZone = useState<string | null>("browser-time-zone", () => null);
onMounted(() => {
browserTimeZone.value = Intl.DateTimeFormat().resolvedOptions().timeZone || null;
});
const formatDateTime = (value: string | Date | null | undefined, options: BrowserDateTimeOptions = DEFAULT_DATE_TIME_OPTIONS) => {
if (!value) return "—";
const date = value instanceof Date ? value : new Date(value);
if (Number.isNaN(date.getTime())) return "—";
return new Intl.DateTimeFormat("ru-RU", {
...options,
timeZone: browserTimeZone.value ?? "UTC"
}).format(date);
};
return {
browserTimeZone,
formatDateTime
};
}