From 9884ca5d883c9e460f3f2fd033573a34f4fd9b92 Mon Sep 17 00:00:00 2001 From: iamkun Date: Thu, 14 Feb 2019 10:57:33 +0800 Subject: [PATCH] fix: Fix bug in customParseFormat plugin while month(MM) is '01' fix #494 --- src/plugin/customParseFormat/index.js | 2 +- test/plugin/customParseFormat.test.js | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/plugin/customParseFormat/index.js b/src/plugin/customParseFormat/index.js index 0bc8ef342..ad639beb1 100644 --- a/src/plugin/customParseFormat/index.js +++ b/src/plugin/customParseFormat/index.js @@ -129,7 +129,7 @@ const parseFormattedInput = (input, format) => { } const now = new Date() return new Date( - year || now.getFullYear(), month - 1 || now.getMonth(), day || now.getDate(), + year || now.getFullYear(), month > 0 ? month - 1 : now.getMonth(), day || now.getDate(), hours || 0, minutes || 0, seconds || 0, milliseconds || 0 ) } catch (e) { diff --git a/test/plugin/customParseFormat.test.js b/test/plugin/customParseFormat.test.js index 835df9674..e26cbea30 100644 --- a/test/plugin/customParseFormat.test.js +++ b/test/plugin/customParseFormat.test.js @@ -24,6 +24,12 @@ it('parse padded string', () => { expect(dayjs(input, format).valueOf()).toBe(moment(input, format).valueOf()) }) +it('parse string January (getMonth() = 0)', () => { + const input = '01/01/2019' + const format = 'DD/MM/YYYY' + expect(dayjs(input, format).valueOf()).toBe(moment(input, format).valueOf()) +}) + it('parse unpadded string', () => { const input = '2.5.18 1:2:3.4 PM -0100' const format = 'D.M.YY H:m:s.S A ZZ'