diff --git a/packages/backend/src/apps/formatter/__tests__/date-time.common.test.ts b/packages/backend/src/apps/formatter/__tests__/date-time.common.test.ts index 4b931999d..b8d729315 100644 --- a/packages/backend/src/apps/formatter/__tests__/date-time.common.test.ts +++ b/packages/backend/src/apps/formatter/__tests__/date-time.common.test.ts @@ -66,7 +66,7 @@ describe('common date-time formatter functions', () => { ) it('supports parsing MyInfo Child date field', () => { - const dateTime = parseDateTime('formsgDateField', '25/03/2024') + const dateTime = parseDateTime('dd/LL/yyyy', '25/03/2024') expect(dateTime.toUnixInteger()).toEqual(1711296000) }) }) diff --git a/packages/backend/src/apps/formatter/__tests__/date-time.format.test.ts b/packages/backend/src/apps/formatter/__tests__/date-time.format.test.ts index 58bf9ce45..b6c2ac001 100644 --- a/packages/backend/src/apps/formatter/__tests__/date-time.format.test.ts +++ b/packages/backend/src/apps/formatter/__tests__/date-time.format.test.ts @@ -71,7 +71,54 @@ describe('convert date time', () => { toFormat: 'dd/LL/yy', expectedResult: '01/04/24', }, - // TODO: add tests + { + inputFormat: 'dd/LL/yy', + inputValue: '01/04/24', + toFormat: 'dd/LL/yyyy', + expectedResult: '01/04/2024', + }, + { + inputFormat: 'dd/LL/yyyy', + inputValue: '01/04/2024', + toFormat: 'dd LLLL yyyy', + expectedResult: '01 April 2024', + }, + { + inputFormat: 'dd LLLL yyyy', + inputValue: '01 April 2024', + toFormat: 'yyyy/LL/dd', + expectedResult: '2024/04/01', + }, + { + inputFormat: 'yyyy/LL/dd', + inputValue: '2024/04/01', + toFormat: 'hh:mm a', + expectedResult: '12:00 am', + }, + { + inputFormat: 'hh:mm a', + inputValue: '11:45 pm', + toFormat: 'hh:mm:ss a', + expectedResult: '11:45:00 pm', + }, + { + inputFormat: 'hh:mm:ss a', + inputValue: '11:45:00 pm', + toFormat: 'hh:mm a', + expectedResult: '11:45 pm', + }, + { + inputFormat: 'dd LLL yyyy hh:mm a', + inputValue: '01 Apr 2024 11:45 pm', + toFormat: 'dd LLL yyyy', + expectedResult: '01 Apr 2024', + }, + { + inputFormat: 'dd LLL yyyy hh:mm:ss a', + inputValue: '01 Apr 2024 11:45:30 pm', + toFormat: 'hh:mm:ss a', + expectedResult: '11:45:30 pm', + }, ])('can handle all supported input formats', (testParams) => { const { inputFormat, inputValue, toFormat, expectedResult } = testParams $.step.parameters = { diff --git a/packages/backend/src/apps/formatter/actions/date-time/common/date-time-format.ts b/packages/backend/src/apps/formatter/actions/date-time/common/date-time-format.ts index 322682d5e..b9f26db51 100644 --- a/packages/backend/src/apps/formatter/actions/date-time/common/date-time-format.ts +++ b/packages/backend/src/apps/formatter/actions/date-time/common/date-time-format.ts @@ -61,14 +61,17 @@ const formatConverters = Object.assign({ stringify: (dateTime: DateTime): string => dateTime.toFormat('dd MMM yyyy'), }, ...Object.fromEntries( - commonDateFormats.map((format) => [ - format, - { - description: format, - parse: (input: string): DateTime => DateTime.fromFormat(input, format), - stringify: (dateTime: DateTime): string => dateTime.toFormat(format), - }, - ]), + commonDateFormats + .filter((format) => format !== 'dd LLL yyyy') // Exclude repeated option due to formsgDateField + .map((format) => [ + format, + { + description: format, + parse: (input: string): DateTime => + DateTime.fromFormat(input, format), + stringify: (dateTime: DateTime): string => dateTime.toFormat(format), + }, + ]), ), }) satisfies Record, DateFormatConverter> @@ -104,7 +107,10 @@ export const field = { description: 'Select this if you are transforming a FormSG date field', value: supportedFormats.enum.formsgDateField, }, - ...commonDateFormatOptions, + // Exclude repeated option due to formsgDateField + ...commonDateFormatOptions.filter( + (option) => option.value !== 'dd LLL yyyy', + ), ], } satisfies IField