Skip to content

Commit

Permalink
fix: customParseFormat supports w ww weekOfYear
Browse files Browse the repository at this point in the history
  • Loading branch information
iamkun committed Aug 9, 2024
1 parent 87ba5b5 commit 0435539
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/plugin/customParseFormat/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { u } from '../localizedFormat/utils'

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

const match1 = /\d/ // 0 - 9
const match2 = /\d\d/ // 00 - 99
Expand Down Expand Up @@ -98,6 +98,8 @@ const expressions = {
}
}
}],
w: [match1to2, addInput('week')],
ww: [match2, addInput('week')],
M: [match1to2, addInput('month')],
MM: [match2, addInput('month')],
MMM: [matchWord, function (input) {
Expand Down Expand Up @@ -176,12 +178,12 @@ function makeParser(format) {
}
}

const parseFormattedInput = (input, format, utc) => {
const parseFormattedInput = (input, format, utc, dayjs) => {
try {
if (['x', 'X'].indexOf(format) > -1) return new Date((format === 'X' ? 1000 : 1) * input)
const parser = makeParser(format)
const {
year, month, day, hours, minutes, seconds, milliseconds, zone
year, month, day, hours, minutes, seconds, milliseconds, zone, week
} = parser(input)
const now = new Date()
const d = day || ((!year && !month) ? now.getDate() : 1)
Expand All @@ -200,7 +202,12 @@ const parseFormattedInput = (input, format, utc) => {
if (utc) {
return new Date(Date.UTC(y, M, d, h, m, s, ms))
}
return new Date(y, M, d, h, m, s, ms)
let newDate
newDate = new Date(y, M, d, h, m, s, ms)
if (week) {
newDate = dayjs(newDate).week(week).toDate()
}
return newDate
} catch (e) {
return new Date('') // Invalid Date
}
Expand Down Expand Up @@ -232,7 +239,7 @@ export default (o, C, d) => {
if (!isStrictWithoutLocale && pl) {
locale = d.Ls[pl]
}
this.$d = parseFormattedInput(date, format, utc)
this.$d = parseFormattedInput(date, format, utc, d)
this.init()
if (pl && pl !== true) this.$L = this.locale(pl).$L
// use != to treat
Expand Down
11 changes: 11 additions & 0 deletions test/plugin/customParseFormat.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import '../../src/locale/zh-cn'
import customParseFormat from '../../src/plugin/customParseFormat'
import advancedFormat from '../../src/plugin/advancedFormat'
import localizedFormats from '../../src/plugin/localizedFormat'
import weekOfYear from '../../src/plugin/weekOfYear'

dayjs.extend(customParseFormat)
dayjs.extend(localizedFormats)
dayjs.extend(weekOfYear) // test parse w, ww

beforeEach(() => {
MockDate.set(new Date())
Expand Down Expand Up @@ -449,3 +451,12 @@ it('parse Q, [Q]', () => {
expect(dayjs(input3, format).valueOf()).toBe(moment(input3, format).valueOf())
expect(dayjs(input4, format).valueOf()).toBe(moment(input4, format).valueOf())
})

it('parse w, ww', () => {
const input = '2024-w1'
const format1 = 'YYYY-[w]w'
expect(dayjs(input, format1).format(format1)).toBe(input)
const input2 = '2024-w32'
const format2 = 'YYYY-[w]ww'
expect(dayjs(input2, format2).format(format1)).toBe(input2)
})

0 comments on commit 0435539

Please sign in to comment.