Updates the logic behind determining if date (#1)

This commit is contained in:
Alicia Sykes 2023-07-15 16:23:54 +01:00
parent 8f89e2670c
commit 6f052d7ac1

View File

@ -60,15 +60,39 @@ const SubRow = styled(StyledRow).attrs({
border-bottom: 1px dashed ${colors.primaryTransparent} !important; border-bottom: 1px dashed ${colors.primaryTransparent} !important;
`; `;
const isValidDate = (date: any): boolean => {
// Checks if a date is within reasonable range
const isInRange = (date: Date): boolean => {
return date >= new Date('1995-01-01') && date <= new Date('2030-12-31');
};
// Check if input is a timestamp
if (typeof date === 'number') {
const timestampDate = new Date(date);
return !isNaN(timestampDate.getTime()) && isInRange(timestampDate);
}
// Check if input is a date string
if (typeof date === 'string') {
const dateStringDate = new Date(date);
return !isNaN(dateStringDate.getTime()) && isInRange(dateStringDate);
}
// Check if input is a Date object
if (date instanceof Date) {
return !isNaN(date.getTime()) && isInRange(date);
}
return false;
};
const formatDate = (dateString: string): string => { const formatDate = (dateString: string): string => {
return new Intl.DateTimeFormat('en-GB', { return new Intl.DateTimeFormat('en-GB', {
day: 'numeric', month: 'long', year: 'numeric' day: 'numeric', month: 'long', year: 'numeric'
}).format(new Date(dateString)); }).format(new Date(dateString));
} }
const formatValue = (value: any): string => { const formatValue = (value: any): string => {
const isValidDate = (date: any) => {
return date instanceof Date && !isNaN(date as any as number) && date >= new Date('1980-01-01');
};
if (isValidDate(new Date(value))) return formatDate(value); if (isValidDate(new Date(value))) return formatDate(value);
if (typeof value === 'boolean') return value ? '✅' : '❌'; if (typeof value === 'boolean') return value ? '✅' : '❌';
return value; return value;