From a0d6a827ce1dd6fa42756f34838254544afcd0bb Mon Sep 17 00:00:00 2001 From: Wasiq Bhamla Date: Sun, 29 Sep 2024 19:55:28 +0300 Subject: [PATCH] fix: :bug: fixed issue with date and time formatting (#19) --- lib/formatting.ts | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/lib/formatting.ts b/lib/formatting.ts index 9fc534a..e80dd15 100644 --- a/lib/formatting.ts +++ b/lib/formatting.ts @@ -15,25 +15,33 @@ export const formatDuration = (duration: number): string => { }; export const formatTime = (dateTime: string): string => { - const cleanedDateString = dateTime.replace('AST', 'UTC'); - const parsedDate = parse( - cleanedDateString, - /* eslint-disable @stylistic/ts/quotes */ - "yyyy-MM-dd'T'HH:mm:ss 'UTC'", - new Date() - ); - return format(parsedDate, 'hh:mm:ss bb'); + const TIME_FORMAT = 'hh:mm:ss bb'; + if (dateTime.endsWith('AST')) { + const cleanedDateString = dateTime.replace('AST', 'UTC'); + const parsedDate = parse( + cleanedDateString, + /* eslint-disable @stylistic/ts/quotes */ + "yyyy-MM-dd'T'HH:mm:ss 'UTC'", + new Date() + ); + return format(parsedDate, TIME_FORMAT); + } + return format(dateTime, TIME_FORMAT); }; export const formatDate = (dateTime: string): string => { - const cleanedDateString = dateTime.replace('AST', 'UTC'); - const parsedDate = parse( - cleanedDateString, - /* eslint-disable @stylistic/ts/quotes */ - "yyyy-MM-dd'T'HH:mm:ss 'UTC'", - new Date() - ); - return format(parsedDate, 'dd-MMM-yyyy'); + const DATE_FORMAT = 'dd-MMM-yyyy'; + if (dateTime.endsWith('AST')) { + const cleanedDateString = dateTime.replace('AST', 'UTC'); + const parsedDate = parse( + cleanedDateString, + /* eslint-disable @stylistic/ts/quotes */ + "yyyy-MM-dd'T'HH:mm:ss 'UTC'", + new Date() + ); + return format(parsedDate, DATE_FORMAT); + } + return format(dateTime, DATE_FORMAT); }; export const round = (value: number): number => {