Skip to content

Commit bf27fda

Browse files
committed
fix: CustomParseFormat plugin parse Do format string
fix #522
1 parent 1768305 commit bf27fda

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/plugin/customParseFormat/index.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const formattingTokens = /(\[[^[]*\])|([-:/.()\s]+)|(A|a|YYYY|YY?|MM?M?M?|DD?|hh?|HH?|mm?|ss?|S{1,3}|z|ZZ?)/g
1+
const formattingTokens = /(\[[^[]*\])|([-:/.()\s]+)|(A|a|YYYY|YY?|MM?M?M?|Do|DD?|hh?|HH?|mm?|ss?|S{1,3}|z|ZZ?)/g
22

33
const match1 = /\d/ // 0 - 9
44
const match2 = /\d\d/ // 00 - 99
@@ -56,6 +56,16 @@ const expressions = {
5656
hh: [match1to2, addInput('hours')],
5757
D: [match1to2, addInput('day')],
5858
DD: [match2, addInput('day')],
59+
Do: [matchWord, function (input) {
60+
const { ordinal } = locale;
61+
[this.day] = input.match(/\d+/)
62+
if (!ordinal) return
63+
for (let i = 1; i <= 31; i += 1) {
64+
if (ordinal(i).replace(/\[|\]/g, '') === input) {
65+
this.day = i
66+
}
67+
}
68+
}],
5969
M: [match1to2, addInput('month')],
6070
MM: [match2, addInput('month')],
6171
MMM: [matchWord, function (input) {

test/plugin/customParseFormat.test.js

+17
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import moment from 'moment'
33
import dayjs from '../../src'
44
import customParseFormat from '../../src/plugin/customParseFormat'
55
import uk from '../../src/locale/uk'
6+
import '../../src/locale/zh-cn'
67

78
dayjs.extend(customParseFormat)
89

@@ -169,3 +170,19 @@ it('correctly parse month from string after changing locale globally', () => {
169170
moment.locale(momentLocale)
170171
}
171172
})
173+
174+
it('correctly parse ordinal', () => {
175+
const input = '7th March 2019'
176+
const input2 = '17th March 2019'
177+
const inputFalse = '7st March 2019'
178+
const inputZHCN = '7日 三月 2019'
179+
const format = 'Do MMMM YYYY'
180+
expect(dayjs(input, format).valueOf())
181+
.toBe(moment(input, format).valueOf())
182+
expect(dayjs(input2, format).valueOf())
183+
.toBe(moment(input2, format).valueOf())
184+
expect(dayjs(inputFalse, format).valueOf())
185+
.toBe(moment(inputFalse, format).valueOf())
186+
expect(dayjs(inputZHCN, format, 'zh-cn').valueOf())
187+
.toBe(moment(inputZHCN, format, 'zh-cn').valueOf())
188+
})

0 commit comments

Comments
 (0)