From c3456ade9b206092cf87dccd046e07bd5c290bc6 Mon Sep 17 00:00:00 2001 From: Viktor Stefanov Date: Thu, 19 Sep 2024 18:51:25 +0300 Subject: [PATCH 1/4] edit formatDate fn to return same date format for both en and bg language --- src/common/util/date.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/common/util/date.ts b/src/common/util/date.ts index 77fd65e99..f6d2bf24a 100644 --- a/src/common/util/date.ts +++ b/src/common/util/date.ts @@ -5,10 +5,13 @@ export const formatDate = 'dd MMM yyyy' export const formatDatetime = 'dd MMM yyyy HH:mm:ss' export const formatDateString = (dateString: string | Date, language?: string) => { - if (language) { - return Intl.DateTimeFormat(language).format(new Date(dateString)) - } - return new Date(dateString).toLocaleDateString() + const date = new Date(dateString) + + const day = String(date.getDate()).padStart(2, '0') + const month = String(date.getMonth() + 1).padStart(2, '0') + const year = date.getFullYear() + + return `${day}.${month}.${year}` } const matchLocale = (language?: string): Locale => { From 6561a9050fab292ab25dcfaeec5978ade19ee671 Mon Sep 17 00:00:00 2001 From: Viktor Stefanov Date: Fri, 20 Sep 2024 11:33:02 +0300 Subject: [PATCH 2/4] edit formatDate fn to return same date format for both en and bg language --- src/common/util/date.ts | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/common/util/date.ts b/src/common/util/date.ts index f6d2bf24a..6173d9c82 100644 --- a/src/common/util/date.ts +++ b/src/common/util/date.ts @@ -5,13 +5,7 @@ export const formatDate = 'dd MMM yyyy' export const formatDatetime = 'dd MMM yyyy HH:mm:ss' export const formatDateString = (dateString: string | Date, language?: string) => { - const date = new Date(dateString) - - const day = String(date.getDate()).padStart(2, '0') - const month = String(date.getMonth() + 1).padStart(2, '0') - const year = date.getFullYear() - - return `${day}.${month}.${year}` + return format(new Date(dateString), 'dd.MM.yyyy') } const matchLocale = (language?: string): Locale => { From a3124e8860bb3e98bc6a74e7231db9582dfee8a5 Mon Sep 17 00:00:00 2001 From: Viktor Stefanov Date: Thu, 3 Oct 2024 10:21:58 +0300 Subject: [PATCH 3/4] removing language as parameter, edit old format fnc invoke --- src/common/util/date.ts | 2 +- src/components/admin/modal/DetailsModal.tsx | 2 +- src/components/client/auth/profile/PersonalInfoTab.tsx | 2 +- src/components/client/blog/DateCreated.tsx | 2 +- src/components/client/campaign-news/CampaignNewsList.tsx | 2 +- src/components/client/campaign-news/SingleArticlePage.tsx | 2 +- .../client/campaign-news/secured/NewsAdminPage.tsx | 6 +++--- src/components/client/campaigns/CampaignNewsSection.tsx | 4 ++-- src/components/common/person/PersonInfo.tsx | 2 +- 9 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/common/util/date.ts b/src/common/util/date.ts index 6173d9c82..8460420ba 100644 --- a/src/common/util/date.ts +++ b/src/common/util/date.ts @@ -4,7 +4,7 @@ import { bg, enUS } from 'date-fns/locale' export const formatDate = 'dd MMM yyyy' export const formatDatetime = 'dd MMM yyyy HH:mm:ss' -export const formatDateString = (dateString: string | Date, language?: string) => { +export const formatDateString = (dateString: string | Date) => { return format(new Date(dateString), 'dd.MM.yyyy') } diff --git a/src/components/admin/modal/DetailsModal.tsx b/src/components/admin/modal/DetailsModal.tsx index 0d96cc5b1..02857c212 100644 --- a/src/components/admin/modal/DetailsModal.tsx +++ b/src/components/admin/modal/DetailsModal.tsx @@ -47,7 +47,7 @@ function DetailsModal() { - {formatDateString(row.row.createdAt, i18n?.language)} + {formatDateString(row.row.createdAt)} {row.row.message || row.row.comment} diff --git a/src/components/client/auth/profile/PersonalInfoTab.tsx b/src/components/client/auth/profile/PersonalInfoTab.tsx index e2c553d97..6fea01b9a 100644 --- a/src/components/client/auth/profile/PersonalInfoTab.tsx +++ b/src/components/client/auth/profile/PersonalInfoTab.tsx @@ -203,7 +203,7 @@ export default function PersonalInfoTab() {

{t('profile:personalInfo.birthday')}

{person?.birthday - ? formatDateString(person?.birthday, i18n?.language) + ? formatDateString(person?.birthday) : t('profile:personalInfo.noBirthday')} diff --git a/src/components/client/blog/DateCreated.tsx b/src/components/client/blog/DateCreated.tsx index 4a6d9c8e0..f97ec74de 100644 --- a/src/components/client/blog/DateCreated.tsx +++ b/src/components/client/blog/DateCreated.tsx @@ -13,7 +13,7 @@ export default function DateCreated({ createdAt, showLabel = false }: Props) { if (!createdAt) return null return ( - {showLabel && t('blog:created-on')} {formatDateString(createdAt, i18n?.language)} + {showLabel && t('blog:created-on')} {formatDateString(createdAt)} ) } diff --git a/src/components/client/campaign-news/CampaignNewsList.tsx b/src/components/client/campaign-news/CampaignNewsList.tsx index a1d5da083..297c8163d 100644 --- a/src/components/client/campaign-news/CampaignNewsList.tsx +++ b/src/components/client/campaign-news/CampaignNewsList.tsx @@ -111,7 +111,7 @@ export default function CampaignNewsList({ articles }: Props) { - {formatDateString(article.publishedAt, i18n?.language)}   + {formatDateString(article.publishedAt)}   {dateToTime(article.publishedAt, i18n?.language)} diff --git a/src/components/client/campaign-news/SingleArticlePage.tsx b/src/components/client/campaign-news/SingleArticlePage.tsx index 287d89e05..ff707c4c1 100644 --- a/src/components/client/campaign-news/SingleArticlePage.tsx +++ b/src/components/client/campaign-news/SingleArticlePage.tsx @@ -96,7 +96,7 @@ export default function SingleArticlePage({ slug }: Props) { - {formatDateString(article.publishedAt, i18n?.language)} + {formatDateString(article.publishedAt)} diff --git a/src/components/client/campaign-news/secured/NewsAdminPage.tsx b/src/components/client/campaign-news/secured/NewsAdminPage.tsx index 8b8970413..52896d24c 100644 --- a/src/components/client/campaign-news/secured/NewsAdminPage.tsx +++ b/src/components/client/campaign-news/secured/NewsAdminPage.tsx @@ -87,7 +87,7 @@ export function NewsAdminPage({ slug, isAdmin }: Props) { flex: 1, minWidth: 140, renderCell: (params: GridRenderCellParams): React.ReactNode => { - return formatDateString(params.row.createdAt, i18n?.language) + return formatDateString(params.row.createdAt) }, }, { @@ -99,7 +99,7 @@ export function NewsAdminPage({ slug, isAdmin }: Props) { minWidth: 200, renderCell: (params: GridRenderCellParams): React.ReactNode => { const date: Date = params.row.publishedAt - return date ? formatDateString(date, i18n?.language) : '' + return date ? formatDateString(date) : '' }, }, { @@ -107,7 +107,7 @@ export function NewsAdminPage({ slug, isAdmin }: Props) { headerName: t('article.lastEdit'), headerClassName: classes.gridColumn, renderCell: (params: GridRenderCellParams): React.ReactNode => { - return params.row.editedAt ? formatDateString(params.row.editedAt, i18n?.language) : '' + return params.row.editedAt ? formatDateString(params.row.editedAt) : '' }, minWidth: 200, flex: 1, diff --git a/src/components/client/campaigns/CampaignNewsSection.tsx b/src/components/client/campaigns/CampaignNewsSection.tsx index 7d7c00dfc..e310c0c5a 100644 --- a/src/components/client/campaigns/CampaignNewsSection.tsx +++ b/src/components/client/campaigns/CampaignNewsSection.tsx @@ -199,7 +199,7 @@ export default function CampaignNewsSection({ campaign, canCreateArticle }: Prop - {`${formatDateString(article.publishedAt, i18n?.language)} ${dateToTime( + {`${formatDateString(article.publishedAt)} ${dateToTime( article.publishedAt, i18n?.language, )}`} @@ -228,7 +228,7 @@ export default function CampaignNewsSection({ campaign, canCreateArticle }: Prop - {`${formatDateString(article.publishedAt, i18n?.language)} ${dateToTime( + {`${formatDateString(article.publishedAt)} ${dateToTime( article.publishedAt, i18n?.language, )}`} diff --git a/src/components/common/person/PersonInfo.tsx b/src/components/common/person/PersonInfo.tsx index cf197a0c5..8f4a7105b 100644 --- a/src/components/common/person/PersonInfo.tsx +++ b/src/components/common/person/PersonInfo.tsx @@ -56,7 +56,7 @@ function PersonInfo({ person }: Props) { - {t('person:info.createdAt')}: {formatDateString(person.createdAt, i18n?.language)} + {t('person:info.createdAt')}: {formatDateString(person.createdAt)} {t('person:info.company')}: {person.company.companyName} From e2639f130675131809748af95c129f66d9b7fe41 Mon Sep 17 00:00:00 2001 From: Viktor Stefanov Date: Fri, 4 Oct 2024 16:26:13 +0300 Subject: [PATCH 4/4] adding new global date view format --- src/common/util/date.ts | 1 + src/components/common/form/FormDatePicker.tsx | 6 +----- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/src/common/util/date.ts b/src/common/util/date.ts index 8460420ba..29562cecf 100644 --- a/src/common/util/date.ts +++ b/src/common/util/date.ts @@ -3,6 +3,7 @@ import { bg, enUS } from 'date-fns/locale' export const formatDate = 'dd MMM yyyy' export const formatDatetime = 'dd MMM yyyy HH:mm:ss' +export const dateViewFormat = 'dd.MM.yyyy' export const formatDateString = (dateString: string | Date) => { return format(new Date(dateString), 'dd.MM.yyyy') diff --git a/src/components/common/form/FormDatePicker.tsx b/src/components/common/form/FormDatePicker.tsx index dad77eb72..47c4dbfd0 100644 --- a/src/components/common/form/FormDatePicker.tsx +++ b/src/components/common/form/FormDatePicker.tsx @@ -2,9 +2,8 @@ import { LocalizationProvider, DatePicker } from '@mui/x-date-pickers' import { AdapterDateFns } from '@mui/x-date-pickers/AdapterDateFns' import { format } from 'date-fns' import { useField, useFormikContext } from 'formik' -import { useTranslation } from 'next-i18next' -import { DATE_VALUE_FORMAT, getDateFormat } from 'common/util/date' +import { DATE_VALUE_FORMAT, dateViewFormat } from 'common/util/date' /** * MUI date picker to be connected with Formik. Propagates updates to the passed Formik field name @@ -15,9 +14,6 @@ import { DATE_VALUE_FORMAT, getDateFormat } from 'common/util/date' export default function FormDatePicker({ name, label }: { name: string; label: string }) { const [field] = useField(name) const { setFieldValue } = useFormikContext() - const { i18n } = useTranslation() - - const dateViewFormat = getDateFormat(i18n?.language) const updateValue = (newValue: Date | null) => { let formattedValue