Skip to content

Commit

Permalink
feat: YYYY-MM-DD ? HH:MM:SS for creationDates
Browse files Browse the repository at this point in the history
  • Loading branch information
vargastat committed Jan 29, 2025
1 parent 9e64ea6 commit 8f617ca
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 7 deletions.
2 changes: 2 additions & 0 deletions src/pages/pegase/home/components/StudyTableHeaders.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import StdAvatar from '@/components/common/layout/stdAvatar/StdAvatar';
import { StudyStatus } from '@/shared/types/common/StudyStatus.type';
import { StudyDTO } from '@/shared/types/pegase/Study.type.ts';
import { formatDateToDDMMYYYY } from '@/shared/utils/dateFormatter';
import { createColumnHelper } from '@tanstack/react-table';
import { useTranslation } from 'react-i18next';
import { RdsRadioButton, RdsTagList } from 'rte-design-system-react';
Expand Down Expand Up @@ -73,6 +74,7 @@ const getStudyTableHeaders = () => {

columnHelper.accessor('creationDate', {
header: t('home.@creation_date'),
cell: ({ getValue }) => formatDateToDDMMYYYY(getValue(), true),
}),
];
};
Expand Down
4 changes: 2 additions & 2 deletions src/pages/pegase/home/pinnedProjects/PinnedProjectCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ const PinnedProjectCards = ({ setShouldRefetchProjectList }: PinnedProjectCardsP
<div className="flex items-center gap-x-0.5 pt-2.5">
<div className="font-sans text-body-xs font-light">
{t('project.@created')} :{' '}
<span className="text-body-xs font-bold">{formatDateToDDMMYYYY(project.creationDate)} </span>{' '}
{t('project.@by')} :
<span className="text-body-xs font-bold">{formatDateToDDMMYYYY(project.creationDate, false)} </span>{' '}
<span className="ml-2">{t('project.@by')}</span> :
</div>

<StdAvatar
Expand Down
2 changes: 1 addition & 1 deletion src/pages/pegase/projects/ProjectContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ const ProjectContent = ({ shouldRefetchProjectList }: ProjectContentProps) => {
<div className="font-sans text-body-xs font-light">
{t('project.@created')} :{' '}
<span className="text-body-xs font-bold">{formatDateToDDMMYYYY(project.creationDate)} </span>{' '}
{t('project.@by')} :
<span className="ml-2">{t('project.@by')}</span> :
</div>
<StdAvatar
size="es"
Expand Down
18 changes: 14 additions & 4 deletions src/shared/utils/dateFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,20 @@ export const standardDateFormatter = (date: Date, locale: string) =>
minute: 'numeric',
});

export const formatDateToDDMMYYYY = (dateInput: Date) => {
export const formatDateToDDMMYYYY = (dateInput: Date | string, includeTime: boolean = false): string => {
const date = typeof dateInput === 'string' ? new Date(dateInput) : dateInput;
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 month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');

if (!includeTime) {
return `${year}-${month}-${day}`;
}

const hours = String(date.getHours()).padStart(2, '0');
const minutes = String(date.getMinutes()).padStart(2, '0');
const seconds = String(date.getSeconds()).padStart(2, '0');

return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
};
21 changes: 21 additions & 0 deletions src/shared/utils/tests/dateFormatter.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/

import { formatDateToDDMMYYYY } from '../dateFormatter';

describe('formatDate', () => {
it('should format date as YYYY-MM-DD when includeTime is false or not provided', () => {
const date = new Date('2024-07-25T10:07:21');
const formattedDate = formatDateToDDMMYYYY(date);
expect(formattedDate).toBe('2024-07-25');
});

it('should format date as YYYY-MM-DD HH:mm:ss when includeTime is true', () => {
const date = new Date('2024-07-25T10:07:21');
const formattedDate = formatDateToDDMMYYYY(date, true);
expect(formattedDate).toBe('2024-07-25 10:07:21');
});
});

0 comments on commit 8f617ca

Please sign in to comment.