Skip to content

Commit 972a4b5

Browse files
fix: restore native Date fallback when strictParsing is false
When strictParsing is false (the default), restore the native Date parsing fallback that was present in v7.x but accidentally removed in v8.0.0. This allows flexible input formats like "12/05/2025" or "2025-12-16 3:31:01 PM" to be parsed even when the dateFormat prop specifies a different format. The fallback only triggers when: 1. strictParsing is false (default) 2. The input is at least 8 characters (to avoid parsing partial inputs) 3. date-fns parsing with the specified format(s) failed This fixes the regression where v8.x required exact format matches even with strictParsing disabled. Fixes #6164 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 1e9e151 commit 972a4b5

2 files changed

Lines changed: 75 additions & 1 deletion

File tree

src/date_utils.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,20 @@ export function parseDate(
318318
return parsedDate;
319319
}
320320
}
321+
322+
// When strictParsing is false, try native Date parsing as a fallback
323+
// This allows flexible input formats like "12/05/2025" or "2025-12-16"
324+
// even when the dateFormat prop specifies a different format.
325+
// Only attempt this for inputs that look like complete dates (minimum
326+
// length of 8 characters, e.g., "1/1/2000") to avoid parsing partial
327+
// inputs like "03/" or "2000" which should be handled by parseDateForNavigation.
328+
if (!strictParsing && value && value.length >= 8) {
329+
const nativeDate = new Date(value);
330+
if (isValidDate(nativeDate)) {
331+
return nativeDate;
332+
}
333+
}
334+
321335
return null;
322336
}
323337

src/test/date_utils_test.test.ts

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1094,7 +1094,10 @@ describe("date_utils", () => {
10941094
expect(parseDate(valueEn, dateFormat, undefined, false)).toEqual(
10951095
expected,
10961096
);
1097-
expect(parseDate(valueEn, dateFormat, locale, false)).toBeNull();
1097+
// With native Date fallback (restored in v8.x), English month names
1098+
// are parsed even when a different locale is specified, because
1099+
// the native Date constructor is locale-agnostic
1100+
expect(parseDate(valueEn, dateFormat, locale, false)).toEqual(expected);
10981101
});
10991102

11001103
it("should not parse date based on locale without a given locale", () => {
@@ -1117,6 +1120,63 @@ describe("date_utils", () => {
11171120

11181121
expect(actual).toEqual(expected);
11191122
});
1123+
1124+
describe("native Date fallback when strictParsing is false (#6164)", () => {
1125+
it("should parse date in different format using native Date fallback", () => {
1126+
// User types MM/dd/yyyy but dateFormat is yyyy-MM-dd
1127+
const value = "12/05/2025";
1128+
const dateFormat = "yyyy-MM-dd";
1129+
1130+
const result = parseDate(value, dateFormat, undefined, false);
1131+
expect(result).not.toBeNull();
1132+
expect(result?.getFullYear()).toBe(2025);
1133+
expect(result?.getMonth()).toBe(11); // December (0-indexed)
1134+
expect(result?.getDate()).toBe(5);
1135+
});
1136+
1137+
it("should parse ISO date string using native Date fallback", () => {
1138+
const value = "2025-12-16";
1139+
const dateFormat = "MM/dd/yyyy";
1140+
1141+
const result = parseDate(value, dateFormat, undefined, false);
1142+
expect(result).not.toBeNull();
1143+
expect(result?.getFullYear()).toBe(2025);
1144+
expect(result?.getMonth()).toBe(11); // December
1145+
expect(result?.getDate()).toBe(16);
1146+
});
1147+
1148+
it("should parse date with time using native Date fallback", () => {
1149+
const value = "2025-12-16 3:31:01 PM";
1150+
const dateFormat = "yyyy-MM-dd hh:mm aa";
1151+
1152+
const result = parseDate(value, dateFormat, undefined, false);
1153+
expect(result).not.toBeNull();
1154+
expect(result?.getFullYear()).toBe(2025);
1155+
expect(result?.getMonth()).toBe(11); // December
1156+
expect(result?.getDate()).toBe(16);
1157+
expect(result?.getHours()).toBe(15); // 3 PM = 15:00
1158+
expect(result?.getMinutes()).toBe(31);
1159+
});
1160+
1161+
it("should NOT use native Date fallback when strictParsing is true", () => {
1162+
const value = "12/05/2025";
1163+
const dateFormat = "yyyy-MM-dd";
1164+
1165+
const result = parseDate(value, dateFormat, undefined, true);
1166+
expect(result).toBeNull();
1167+
});
1168+
1169+
it("should prefer exact format match over native Date fallback", () => {
1170+
const value = "2025-12-05";
1171+
const dateFormat = "yyyy-MM-dd";
1172+
1173+
const result = parseDate(value, dateFormat, undefined, false);
1174+
expect(result).not.toBeNull();
1175+
expect(result?.getFullYear()).toBe(2025);
1176+
expect(result?.getMonth()).toBe(11); // December
1177+
expect(result?.getDate()).toBe(5);
1178+
});
1179+
});
11201180
});
11211181

11221182
describe("isMonthInRange", () => {

0 commit comments

Comments
 (0)