Add support for DD-MM-YYYY date format in toISO function and update tests (fixes #19)

This commit is contained in:
2025-10-27 14:59:14 -04:00
parent fd29899f4e
commit 2a92375116
2 changed files with 26 additions and 1 deletions
+17
View File
@@ -27,3 +27,20 @@ test("toISO parses ISO and common whois formats", () => {
const plus0530 = toISO("2025-03-23T10:53:03+05:30");
expect(plus0530).toBe("2025-03-23T05:23:03Z");
});
test("toISO parses DD-MM-YYYY format (used by .il and .hk)", () => {
// Test the example from the issue
const ddmmyyyy1 = toISO("21-07-2026");
expect(ddmmyyyy1).toBe("2026-07-21T00:00:00Z");
// Test edge cases
const ddmmyyyy2 = toISO("01-01-2025");
expect(ddmmyyyy2).toBe("2025-01-01T00:00:00Z");
const ddmmyyyy3 = toISO("31-12-2025");
expect(ddmmyyyy3).toBe("2025-12-31T00:00:00Z");
// Ensure DD-MMM-YYYY (with month name) still works
const dmmmy = toISO("02-Jan-2023");
expect(dmmmy).toBe("2023-01-02T00:00:00Z");
});
+9 -1
View File
@@ -16,6 +16,8 @@ export function toISO(
/^(\d{4})\/(\d{2})\/(\d{2})[ T](\d{2}):(\d{2}):(\d{2})(?:Z|([+-]\d{2})(?::?(\d{2}))?)?$/,
// 02-Jan-2023
/^(\d{2})-([A-Za-z]{3})-(\d{4})$/,
// 21-07-2026 (DD-MM-YYYY used by .il, .hk)
/^(\d{2})-(\d{2})-(\d{4})$/,
// Jan 02 2023
/^([A-Za-z]{3})\s+(\d{1,2})\s+(\d{4})$/,
];
@@ -93,9 +95,15 @@ function parseDateWithRegex(
}
return new Date(dt);
}
// If the matched string contains hyphens, treat as DD-MMM-YYYY
// If the matched string contains hyphens, check if numeric (DD-MM-YYYY) or alpha (DD-MMM-YYYY)
if (m[0].includes("-")) {
const [_, dd, monStr, yyyy] = m;
// Check if month component is numeric (DD-MM-YYYY) or alphabetic (DD-MMM-YYYY)
if (/^\d+$/.test(monStr)) {
// DD-MM-YYYY format (e.g., 21-07-2026)
return new Date(Date.UTC(Number(yyyy), Number(monStr) - 1, Number(dd)));
}
// DD-MMM-YYYY format (e.g., 02-Jan-2023)
const mon = monthMap[monStr.toLowerCase()];
return new Date(Date.UTC(Number(yyyy), mon, Number(dd)));
}