Skip to content

Commit 75a4ed1

Browse files
Merge pull request #6172 from Hacker0x01/fix/restore-native-date-fallback-6164
fix: restore native Date fallback when strictParsing is false
2 parents d4625d4 + 972a4b5 commit 75a4ed1

File tree

2 files changed

+75
-1
lines changed

2 files changed

+75
-1
lines changed

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)