Skip to content

Commit 98a3b67

Browse files
committed
fix: Fix CustomParseFormat plugin month index error
fix #915
1 parent a08756e commit 98a3b67

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

src/plugin/customParseFormat/index.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -78,19 +78,19 @@ const expressions = {
7878
MMM: [matchWord, function (input) {
7979
const months = getLocalePart('months')
8080
const monthsShort = getLocalePart('monthsShort')
81-
const matchIndex = (monthsShort || months.map(_ => _.substr(0, 3))).indexOf(input)
82-
if (matchIndex < 0) {
81+
const matchIndex = (monthsShort || months.map(_ => _.substr(0, 3))).indexOf(input) + 1
82+
if (matchIndex < 1) {
8383
throw new Error()
8484
}
85-
this.month = (matchIndex + 1) % 12
85+
this.month = (matchIndex % 12) || matchIndex
8686
}],
8787
MMMM: [matchWord, function (input) {
8888
const months = getLocalePart('months')
89-
const matchIndex = months.indexOf(input)
90-
if (matchIndex < 0) {
89+
const matchIndex = months.indexOf(input) + 1
90+
if (matchIndex < 1) {
9191
throw new Error()
9292
}
93-
this.month = (matchIndex + 1) % 12
93+
this.month = (matchIndex % 12) || matchIndex
9494
}],
9595
Y: [matchSigned, addInput('year')],
9696
YY: [match2, function (input) {

test/plugin/customParseFormat.test.js

+6
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ it('parse string for MMM month format', () => {
3131
const input = '4/Mar/2019:11:16:26 +0800'
3232
const format = 'D/MMM/YYYY:H:m:s zz'
3333
expect(dayjs(input, format).valueOf()).toBe(moment(input, format).valueOf())
34+
const input2 = '21-Dec-18'
35+
const format2 = 'D-MMM-YY'
36+
expect(dayjs(input2, format2).valueOf()).toBe(moment(input2, format2).valueOf())
3437
})
3538

3639
it('parse string January (getMonth() = 0)', () => {
@@ -135,6 +138,9 @@ it('parse month from string', () => {
135138
const input = '2018 February 03'
136139
const format = 'YYYY MMMM DD'
137140
expect(dayjs(input, format).valueOf()).toBe(moment(input, format).valueOf())
141+
const input2 = '21-December-18'
142+
const format2 = 'D-MMMM-YY'
143+
expect(dayjs(input2, format2).valueOf()).toBe(moment(input2, format2).valueOf())
138144
})
139145

140146
it('parse month from short string', () => {

0 commit comments

Comments
 (0)