35 lines
935 B
TypeScript
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
|
|
};
|
|
}
|