Skip to content

Commit 3cea04d

Browse files
authored
fix: Fix CustomParseFormat plugin of parsing only YYYY / YYYY-MM bug (#873)
fix #849
1 parent 62b092d commit 3cea04d

2 files changed

Lines changed: 26 additions & 8 deletions

File tree

src/plugin/customParseFormat/index.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -151,21 +151,20 @@ const parseFormattedInput = (input, format, utc) => {
151151
const {
152152
year, month, day, hours, minutes, seconds, milliseconds, zone
153153
} = parser(input)
154-
if (zone) {
155-
return new Date(Date.UTC(
156-
year, month - 1, day,
157-
hours || 0,
158-
minutes || 0, seconds || 0, milliseconds || 0
159-
) + (zone.offset * 60 * 1000))
160-
}
161154
const now = new Date()
162155
const d = day || ((!year && !month) ? now.getDate() : 1)
163156
const y = year || now.getFullYear()
164-
const M = month > 0 ? month - 1 : now.getMonth()
157+
let M = 0
158+
if (!(year && !month)) {
159+
M = month > 0 ? month - 1 : now.getMonth()
160+
}
165161
const h = hours || 0
166162
const m = minutes || 0
167163
const s = seconds || 0
168164
const ms = milliseconds || 0
165+
if (zone) {
166+
return new Date(Date.UTC(y, M, d, h, m, s, ms + (zone.offset * 60 * 1000)))
167+
}
169168
if (utc) {
170169
return new Date(Date.UTC(y, M, d, h, m, s, ms))
171170
}

test/plugin/customParseFormat.test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,25 @@ it('parse HH:mm:ss but only one digit', () => {
9898
expect(dayjs(input, format).valueOf()).toBe(moment(input, format).valueOf())
9999
})
100100

101+
describe('parse YYYY / YYYY-MM only', () => {
102+
it('YYYY', () => {
103+
const input = '2001 +08:00'
104+
const format = 'YYYY Z'
105+
expect(dayjs(input, format).valueOf()).toBe(moment(input, format).valueOf())
106+
const input2 = '2001'
107+
const format2 = 'YYYY'
108+
expect(dayjs(input2, format2).valueOf()).toBe(moment(input2, format2).valueOf())
109+
})
110+
it('YYYY-MM', () => {
111+
const input = '2001-01 +08:00'
112+
const format = 'YYYY-MM Z'
113+
expect(dayjs(input, format).valueOf()).toBe(moment(input, format).valueOf())
114+
const input2 = '2001-01'
115+
const format2 = 'YYYY-MM'
116+
expect(dayjs(input2, format2).valueOf()).toBe(moment(input2, format2).valueOf())
117+
})
118+
})
119+
101120
it('parse hh:mm:ss but only one digit', () => {
102121
const input = '0:0:1'
103122
const format = 'hh:mm:ss'

0 commit comments

Comments
 (0)