From 7bd97328ace1b547e9f59170d85a66a9d068f087 Mon Sep 17 00:00:00 2001 From: oliverpool <3864879+oliverpool@users.noreply.github.com> Date: Wed, 7 Feb 2024 20:26:28 +0100 Subject: [PATCH] feat: fallback to date-fns.parseISO when date cannot be parsed (#1080) --- packages/core/src/widgets/datetime/DateTimeControl.tsx | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/core/src/widgets/datetime/DateTimeControl.tsx b/packages/core/src/widgets/datetime/DateTimeControl.tsx index 0246354d..c3c03305 100644 --- a/packages/core/src/widgets/datetime/DateTimeControl.tsx +++ b/packages/core/src/widgets/datetime/DateTimeControl.tsx @@ -82,7 +82,14 @@ const DateTimeControl: FC> = ({ return valueToParse; } - return storageFormat ? parse(valueToParse, storageFormat, new Date()) : parseISO(valueToParse); + if (storageFormat) { + const parsed = parse(valueToParse, storageFormat, new Date()); + // if parsing fails, Invalid Date (NaN) will be returned: fallback to parseISO + if (!isNaN(parsed.getTime())) { + return parsed; + } + } + return parseISO(valueToParse); }, [defaultValue, storageFormat, internalValue]); const handleChange = useCallback(