Skip to content

Commit

Permalink
fix(localize): parseDate by default to 2000 instead of 1900 when date…
Browse files Browse the repository at this point in the history
… is below 49 (#2148)
  • Loading branch information
gerjanvangeest authored Nov 29, 2023
1 parent bf78222 commit 9b5edf3
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/grumpy-bags-give.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@lion/ui': patch
---

[localize] parseDate by default to 2000 instead of 1900 when date is below 49
6 changes: 5 additions & 1 deletion packages/ui/components/localize/src/date/parseDate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 &&
Expand Down
6 changes: 6 additions & 0 deletions packages/ui/components/localize/test/date/parseDate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 9b5edf3

Please sign in to comment.