Skip to content

Commit 8ca74f1

Browse files
authored
fix: customParseFormat supports Q quter / w ww weekOfYear (#2705)
* fix: customParseFormat supports Q quter * fix: customParseFormat supports w ww weekOfYear
1 parent 0898d9f commit 8ca74f1

File tree

2 files changed

+39
-6
lines changed

2 files changed

+39
-6
lines changed

src/plugin/customParseFormat/index.js

+16-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { u } from '../localizedFormat/utils'
22

3-
const formattingTokens = /(\[[^[]*\])|([-_:/.,()\s]+)|(A|a|YYYY|YY?|MM?M?M?|Do|DD?|hh?|HH?|mm?|ss?|S{1,3}|z|ZZ?)/g
3+
const formattingTokens = /(\[[^[]*\])|([-_:/.,()\s]+)|(A|a|Q|YYYY|YY?|ww?|MM?M?M?|Do|DD?|hh?|HH?|mm?|ss?|S{1,3}|z|ZZ?)/g
44

55
const match1 = /\d/ // 0 - 9
66
const match2 = /\d\d/ // 00 - 99
@@ -66,6 +66,9 @@ const expressions = {
6666
a: [matchWord, function (input) {
6767
this.afternoon = meridiemMatch(input, true)
6868
}],
69+
Q: [match1, function (input) {
70+
this.month = ((input - 1) * 3) + 1
71+
}],
6972
S: [match1, function (input) {
7073
this.milliseconds = +input * 100
7174
}],
@@ -95,6 +98,8 @@ const expressions = {
9598
}
9699
}
97100
}],
101+
w: [match1to2, addInput('week')],
102+
ww: [match2, addInput('week')],
98103
M: [match1to2, addInput('month')],
99104
MM: [match2, addInput('month')],
100105
MMM: [matchWord, function (input) {
@@ -173,12 +178,12 @@ function makeParser(format) {
173178
}
174179
}
175180

176-
const parseFormattedInput = (input, format, utc) => {
181+
const parseFormattedInput = (input, format, utc, dayjs) => {
177182
try {
178183
if (['x', 'X'].indexOf(format) > -1) return new Date((format === 'X' ? 1000 : 1) * input)
179184
const parser = makeParser(format)
180185
const {
181-
year, month, day, hours, minutes, seconds, milliseconds, zone
186+
year, month, day, hours, minutes, seconds, milliseconds, zone, week
182187
} = parser(input)
183188
const now = new Date()
184189
const d = day || ((!year && !month) ? now.getDate() : 1)
@@ -197,7 +202,12 @@ const parseFormattedInput = (input, format, utc) => {
197202
if (utc) {
198203
return new Date(Date.UTC(y, M, d, h, m, s, ms))
199204
}
200-
return new Date(y, M, d, h, m, s, ms)
205+
let newDate
206+
newDate = new Date(y, M, d, h, m, s, ms)
207+
if (week) {
208+
newDate = dayjs(newDate).week(week).toDate()
209+
}
210+
return newDate
201211
} catch (e) {
202212
return new Date('') // Invalid Date
203213
}
@@ -224,12 +234,12 @@ export default (o, C, d) => {
224234
const isStrictWithLocale = args[3] === true
225235
const isStrict = isStrictWithoutLocale || isStrictWithLocale
226236
let pl = args[2]
227-
if (isStrictWithLocale) [,, pl] = args
237+
if (isStrictWithLocale) [, , pl] = args
228238
locale = this.$locale()
229239
if (!isStrictWithoutLocale && pl) {
230240
locale = d.Ls[pl]
231241
}
232-
this.$d = parseFormattedInput(date, format, utc)
242+
this.$d = parseFormattedInput(date, format, utc, d)
233243
this.init()
234244
if (pl && pl !== true) this.$L = this.locale(pl).$L
235245
// use != to treat

test/plugin/customParseFormat.test.js

+23
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ import '../../src/locale/zh-cn'
77
import customParseFormat from '../../src/plugin/customParseFormat'
88
import advancedFormat from '../../src/plugin/advancedFormat'
99
import localizedFormats from '../../src/plugin/localizedFormat'
10+
import weekOfYear from '../../src/plugin/weekOfYear'
1011

1112
dayjs.extend(customParseFormat)
1213
dayjs.extend(localizedFormats)
14+
dayjs.extend(weekOfYear) // test parse w, ww
1315

1416
beforeEach(() => {
1517
MockDate.set(new Date())
@@ -437,3 +439,24 @@ it('parse X x', () => {
437439
dayjs.extend(advancedFormat)
438440
expect(dayjs(input2, format2, true).valueOf()).toBe(moment(input2, format2, true).valueOf())
439441
})
442+
443+
it('parse Q, [Q]', () => {
444+
const input1 = '2024-Q1'
445+
const input2 = '2024-Q2'
446+
const input3 = '2024-Q3'
447+
const input4 = '2024-Q4'
448+
const format = 'YYYY-[Q]Q'
449+
expect(dayjs(input1, format).valueOf()).toBe(moment(input1, format).valueOf())
450+
expect(dayjs(input2, format).valueOf()).toBe(moment(input2, format).valueOf())
451+
expect(dayjs(input3, format).valueOf()).toBe(moment(input3, format).valueOf())
452+
expect(dayjs(input4, format).valueOf()).toBe(moment(input4, format).valueOf())
453+
})
454+
455+
it('parse w, ww', () => {
456+
const input = '2024-w1'
457+
const format1 = 'YYYY-[w]w'
458+
expect(dayjs(input, format1).format(format1)).toBe(input)
459+
const input2 = '2024-w32'
460+
const format2 = 'YYYY-[w]ww'
461+
expect(dayjs(input2, format2).format(format1)).toBe(input2)
462+
})

0 commit comments

Comments
 (0)