feat: datetimes with timezones
This commit is contained in:
@ -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)}
|
||||
|
||||
@ -178,6 +178,10 @@ export type DealSchema = {
|
||||
* Statusid
|
||||
*/
|
||||
statusId: number;
|
||||
/**
|
||||
* Createdat
|
||||
*/
|
||||
createdAt: string;
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@ -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
16
src/utils/datetime.ts
Normal 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));
|
||||
};
|
||||
Reference in New Issue
Block a user