fix(localize): parseDate by default to 2000 instead of 1900 when date is below 49 (#2148)
This commit is contained in:
parent
bf782223a7
commit
9b5edf30fb
3 changed files with 16 additions and 1 deletions
5
.changeset/grumpy-bags-give.md
Normal file
5
.changeset/grumpy-bags-give.md
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
'@lion/ui': patch
|
||||
---
|
||||
|
||||
[localize] parseDate by default to 2000 instead of 1900 when date is below 49
|
||||
|
|
@ -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 &&
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
Loading…
Reference in a new issue