init
This commit is contained in:
34
frontend/composables/useBrowserDateTime.ts
Normal file
34
frontend/composables/useBrowserDateTime.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
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
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user