fix(localize): parseDate by default to 2000 instead of 1900 when date is below 49 (#2148)

This commit is contained in:
gerjanvangeest 2023-11-29 10:52:21 +01:00 committed by GitHub
parent bf782223a7
commit 9b5edf30fb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 1 deletions

View file

@ -0,0 +1,5 @@
---
'@lion/ui': patch
---
[localize] parseDate by default to 2000 instead of 1900 when date is below 49

View file

@ -58,8 +58,12 @@ export function parseDate(dateString) {
}
const [year, month, day] = parsedString.split('/').map(Number);
const parsedDate = new Date(new Date(year, month - 1, day));
let correctedYear = year;
if (year < 50) {
correctedYear = 2000 + year;
}
const parsedDate = new Date(new Date(correctedYear, month - 1, day));
// Check if parsedDate is not `Invalid Date` or that the date has changed (e.g. the not existing 31.02.2020)
if (
year > 0 &&

View file

@ -23,6 +23,12 @@ describe('parseDate()', () => {
localizeTearDown();
});
it('handles year correctly', () => {
expect(equalsDate(parseDate('1/11/1'), new Date('2001/11/01'))).to.equal(true);
expect(equalsDate(parseDate('1/11/49'), new Date('2049/11/01'))).to.equal(true);
expect(equalsDate(parseDate('1/11/50'), new Date('1950/11/01'))).to.equal(true);
});
it('adds leading zeros', () => {
expect(equalsDate(parseDate('1-1-1979'), new Date('1979/01/01'))).to.equal(true);
expect(equalsDate(parseDate('1-11-1979'), new Date('1979/11/01'))).to.equal(true);