From 3c1cf4380d941511616cd703bb8c1cd1c669b4e4 Mon Sep 17 00:00:00 2001 From: jasquat <2487833+jasquat@users.noreply.github.com> Date: Tue, 2 Jan 2024 10:16:00 -0500 Subject: [PATCH] feature/support-more-date-formats (#835) * added support for Month names and abbreviations in frontend * corrections pointed out by coderabbit --------- Co-authored-by: jasquat --- spiffworkflow-frontend/src/config.tsx | 21 +++++++++++++------ .../src/services/DateAndTimeService.tsx | 15 +++---------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/spiffworkflow-frontend/src/config.tsx b/spiffworkflow-frontend/src/config.tsx index fd7750c73..2df904be1 100644 --- a/spiffworkflow-frontend/src/config.tsx +++ b/spiffworkflow-frontend/src/config.tsx @@ -91,16 +91,25 @@ if ( ) { generalDateFormat = window.spiffworkflowFrontendJsenv.DATE_FORMAT; } -const supportedDateFormats = ['yyyy-MM-dd', 'dd-MM-yyyy', 'MM-dd-yyyy']; -if (!supportedDateFormats.includes(generalDateFormat)) { + +const splitDateFormat = generalDateFormat.split('-'); +const supportedDateFormatTypes = ['yyyy', 'MM', 'MMM', 'MMMM', 'dd']; +const unsupportedFormatTypes = splitDateFormat.filter( + (x) => !supportedDateFormatTypes.includes(x) +); +if (unsupportedFormatTypes.length > 0) { throw new Error( - `Given SPIFFWORKFLOW_FRONTEND_RUNTIME_CONFIG_DATE_FORMAT is not supported. Given: ${generalDateFormat}. Valid options are: ${supportedDateFormats}` + `Given SPIFFWORKFLOW_FRONTEND_RUNTIME_CONFIG_DATE_FORMAT is not supported. Given: ${generalDateFormat} with invalid options: ${unsupportedFormatTypes.join( + ', ' + )}. Valid options are: ${supportedDateFormatTypes.join(', ')}` ); } const carbonDateFormat = generalDateFormat - .replace('yyyy', 'Y') - .replace('MM', 'm') - .replace('dd', 'd'); + .replace(/\byyyy\b/, 'Y') + .replace(/\bMM\b/, 'm') + .replace(/\bMMM\b/, 'M') + .replace(/\bMMMM\b/, 'F') + .replace(/\bdd\b/, 'd'); export const DATE_TIME_FORMAT = `${generalDateFormat} HH:mm:ss`; export const TIME_FORMAT_HOURS_MINUTES = 'HH:mm'; export const DATE_FORMAT = generalDateFormat; diff --git a/spiffworkflow-frontend/src/services/DateAndTimeService.tsx b/spiffworkflow-frontend/src/services/DateAndTimeService.tsx index 69e98db6d..e87f3ae12 100644 --- a/spiffworkflow-frontend/src/services/DateAndTimeService.tsx +++ b/spiffworkflow-frontend/src/services/DateAndTimeService.tsx @@ -1,4 +1,4 @@ -import { Duration, format } from 'date-fns'; +import { Duration, format, parse } from 'date-fns'; import { DATE_TIME_FORMAT, DATE_FORMAT, @@ -41,17 +41,8 @@ const convertDateObjectToFormattedString = (dateObject: Date) => { }; const dateStringToYMDFormat = (dateString: string) => { - if (dateString && dateString.match(/^\d{2}-\d{2}-\d{4}$/)) { - if (DATE_FORMAT.startsWith('dd')) { - const d = dateString.split('-'); - return `${d[2]}-${d[1]}-${d[0]}`; - } - if (DATE_FORMAT.startsWith('MM')) { - const d = dateString.split('-'); - return `${d[2]}-${d[0]}-${d[1]}`; - } - } - return dateString; + const newDate = parse(dateString, DATE_FORMAT, new Date()); + return format(newDate, 'yyyy-MM-dd'); }; const convertDateAndTimeStringsToDate = (