From fbca31402659571fb7a6bd63e1a3cdd52786be2e Mon Sep 17 00:00:00 2001 From: Mohammed Abad Date: Tue, 16 Apr 2024 10:18:35 +0100 Subject: [PATCH] reworked the date format pipe --- .../pipes/date-format/date-format.pipe.ts | 28 +++++++++++++------ 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/frontend/src/app/shared/pipes/date-format/date-format.pipe.ts b/frontend/src/app/shared/pipes/date-format/date-format.pipe.ts index c847199..62271f2 100644 --- a/frontend/src/app/shared/pipes/date-format/date-format.pipe.ts +++ b/frontend/src/app/shared/pipes/date-format/date-format.pipe.ts @@ -1,16 +1,28 @@ import { Pipe, PipeTransform } from '@angular/core'; +import { format } from 'date-fns'; @Pipe({ - name: 'dateFormat' + name: 'dateFormat', }) export class DateFormatPipe implements PipeTransform { + // transform(value: string): string { + // if (!value) return ''; - transform(value: string): string { - if (!value) return ''; - - const date = new Date(value); - const formatter = new Intl.DateTimeFormat('en-US', { month: 'long', day: '2-digit', year: 'numeric' }); - return formatter.format(date); + // const date = new Date(value); + // const formatter = new Intl.DateTimeFormat('en-US', { month: 'long', day: '2-digit', year: 'numeric' }); + // return formatter.format(date); + // } + + transform( + value: Date | string | number, + formatString: string = 'yyyy-MM-dd' + ): string { + const date = value instanceof Date ? value : new Date(value); + if (!date.getTime()) { + console.log('Invalid date provided to customDateFormat'); + return ''; + } + + return format(date, formatString); } - }