feat: datetimes with timezones

This commit is contained in:
2025-08-24 14:54:10 +04:00
parent d5be9ce61a
commit e5602551c5
7 changed files with 53 additions and 4 deletions

View File

@ -1,9 +1,10 @@
import { FC, useState } from "react";
import { isEqual } from "lodash";
import { Button, Group, Stack, TextInput } from "@mantine/core";
import { Button, Group, Stack, Text, TextInput } from "@mantine/core";
import { useForm } from "@mantine/form";
import { DealsCrud } from "@/hooks/cruds/useDealsCrud";
import { DealSchema } from "@/lib/client";
import { utcDateTimeToLocalString } from "@/utils/datetime";
type Props = {
dealsCrud: DealsCrud;
@ -36,8 +37,11 @@ const GeneralTab: FC<Props> = ({ deal, dealsCrud, onClose }) => {
label={"Название"}
{...form.getInputProps("name")}
/>
<Group justify={"space-between"}>
<Group>
<Text>Создано: {utcDateTimeToLocalString(deal.createdAt)}</Text>
<Group
justify={"space-between"}
wrap={"nowrap"}>
<Group wrap={"nowrap"}>
<Button
type={"submit"}
disabled={isEqual(form.values, initialValues)}

View File

@ -178,6 +178,10 @@ export type DealSchema = {
* Statusid
*/
statusId: number;
/**
* Createdat
*/
createdAt: string;
};
/**

View File

@ -60,6 +60,9 @@ export const zDealSchema = z.object({
name: z.string(),
lexorank: z.string(),
statusId: z.int(),
createdAt: z.iso.datetime({
offset: true,
}),
});
/**

16
src/utils/datetime.ts Normal file
View File

@ -0,0 +1,16 @@
import { formatInTimeZone } from "date-fns-tz";
export const utcDateToLocal = (datetime: string | Date) => {
const userTZ = Intl.DateTimeFormat().resolvedOptions().timeZone;
const localTime = formatInTimeZone(datetime, userTZ, "yyyy-MM-dd HH:mm:ss");
return new Date(localTime);
};
export const localDateTimeToString = (datetime: string | Date) => {
const date = new Date(datetime);
return date.toLocaleString("ru").substring(0, 17);
};
export const utcDateTimeToLocalString = (datetime: string | Date) => {
return localDateTimeToString(utcDateToLocal(datetime));
};