feature/support-more-date-formats (#835)

* added support for Month names and abbreviations in frontend

* corrections pointed out by coderabbit

---------

Co-authored-by: jasquat <jasquat@users.noreply.github.com>
This commit is contained in:
jasquat 2024-01-02 10:16:00 -05:00 committed by GitHub
parent b1206c5b3d
commit 3c1cf4380d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 18 deletions

View File

@ -91,16 +91,25 @@ if (
) { ) {
generalDateFormat = window.spiffworkflowFrontendJsenv.DATE_FORMAT; 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( 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 const carbonDateFormat = generalDateFormat
.replace('yyyy', 'Y') .replace(/\byyyy\b/, 'Y')
.replace('MM', 'm') .replace(/\bMM\b/, 'm')
.replace('dd', 'd'); .replace(/\bMMM\b/, 'M')
.replace(/\bMMMM\b/, 'F')
.replace(/\bdd\b/, 'd');
export const DATE_TIME_FORMAT = `${generalDateFormat} HH:mm:ss`; export const DATE_TIME_FORMAT = `${generalDateFormat} HH:mm:ss`;
export const TIME_FORMAT_HOURS_MINUTES = 'HH:mm'; export const TIME_FORMAT_HOURS_MINUTES = 'HH:mm';
export const DATE_FORMAT = generalDateFormat; export const DATE_FORMAT = generalDateFormat;

View File

@ -1,4 +1,4 @@
import { Duration, format } from 'date-fns'; import { Duration, format, parse } from 'date-fns';
import { import {
DATE_TIME_FORMAT, DATE_TIME_FORMAT,
DATE_FORMAT, DATE_FORMAT,
@ -41,17 +41,8 @@ const convertDateObjectToFormattedString = (dateObject: Date) => {
}; };
const dateStringToYMDFormat = (dateString: string) => { const dateStringToYMDFormat = (dateString: string) => {
if (dateString && dateString.match(/^\d{2}-\d{2}-\d{4}$/)) { const newDate = parse(dateString, DATE_FORMAT, new Date());
if (DATE_FORMAT.startsWith('dd')) { return format(newDate, 'yyyy-MM-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 convertDateAndTimeStringsToDate = ( const convertDateAndTimeStringsToDate = (