From eb087f52861313b8dd8a5c1b77858665ec72859e Mon Sep 17 00:00:00 2001 From: iamkun Date: Tue, 6 Jul 2021 15:32:50 +0800 Subject: [PATCH] fix: customParseFormat support parsing X x timestamp (#1567) --- src/plugin/customParseFormat/index.js | 1 + test/plugin/customParseFormat.test.js | 9 +++++++++ 2 files changed, 10 insertions(+) diff --git a/src/plugin/customParseFormat/index.js b/src/plugin/customParseFormat/index.js index 536a6654..73e48eb9 100644 --- a/src/plugin/customParseFormat/index.js +++ b/src/plugin/customParseFormat/index.js @@ -175,6 +175,7 @@ function makeParser(format) { const parseFormattedInput = (input, format, utc) => { try { + if (['x', 'X'].indexOf(format) > -1) return new Date((format === 'X' ? 1000 : 1) * input) const parser = makeParser(format) const { year, month, day, hours, minutes, seconds, milliseconds, zone diff --git a/test/plugin/customParseFormat.test.js b/test/plugin/customParseFormat.test.js index 65a70299..97fe8fb7 100644 --- a/test/plugin/customParseFormat.test.js +++ b/test/plugin/customParseFormat.test.js @@ -368,3 +368,12 @@ it('custom two-digit year parse function', () => { const input3 = '99-05-02' expect(dayjs(input3, format).year()).toBe(1899) }) + +it('parse X x', () => { + const input = '1410715640.579' + const format = 'X' + expect(dayjs(input, format).valueOf()).toBe(moment(input, format).valueOf()) + const input2 = '1410715640579' + const format2 = 'x' + expect(dayjs(input2, format2).valueOf()).toBe(moment(input2, format2).valueOf()) +})