Skip to content

Commit

Permalink
feat: add customParseFormat support for isoweek, the usage is consist…
Browse files Browse the repository at this point in the history
…ent with moment
  • Loading branch information
lizhihang committed Feb 17, 2025
1 parent 3738cc1 commit 15a18f3
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
18 changes: 15 additions & 3 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?|ww?|MM?M?M?|Do|DD?|hh?|HH?|mm?|ss?|S{1,3}|z|ZZ?)/g
const formattingTokens = /(\[[^[]*\])|([-_:/.,()\s]+)|(A|a|Q|YYYY|YY?|ww?|GGGG|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 @@ -100,6 +100,9 @@ const expressions = {
}],
w: [match1to2, addInput('week')],
ww: [match2, addInput('week')],
W: [match1to2, addInput('isoWeek')],
WW: [match2, addInput('isoWeek')],
GGGG: [match4, addInput('isoYear')],
M: [match1to2, addInput('month')],
MM: [match2, addInput('month')],
MMM: [matchWord, function (input) {
Expand Down Expand Up @@ -182,9 +185,15 @@ 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 parserResult = parser(input)
const {
year, month, day, hours, minutes, seconds, milliseconds, zone, week
} = parser(input)
month, day, hours, minutes, seconds, milliseconds, zone, week, isoYear
} = parserResult
let { year, isoWeek } = parserResult
year = year || isoYear
if (isoYear && !isoWeek) {
isoWeek = 1
}
const now = new Date()
const d = day || ((!year && !month) ? now.getDate() : 1)
const y = year || now.getFullYear()
Expand All @@ -206,6 +215,9 @@ const parseFormattedInput = (input, format, utc, dayjs) => {
newDate = new Date(y, M, d, h, m, s, ms)
if (week) {
newDate = dayjs(newDate).week(week).toDate()
} else if (isoWeek) {
newDate = dayjs(newDate).add(7, 'day').isoWeek(isoWeek).isoWeekday(1)
.toDate()
}
return newDate
} catch (e) {
Expand Down
25 changes: 25 additions & 0 deletions test/plugin/customParseFormat.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import customParseFormat from '../../src/plugin/customParseFormat'
import advancedFormat from '../../src/plugin/advancedFormat'
import localizedFormats from '../../src/plugin/localizedFormat'
import weekOfYear from '../../src/plugin/weekOfYear'
import isoWeek from '../../src/plugin/isoWeek'

dayjs.extend(customParseFormat)
dayjs.extend(localizedFormats)
dayjs.extend(weekOfYear) // test parse w, ww
dayjs.extend(isoWeek) // test parse GGGG; parse W, WW

beforeEach(() => {
MockDate.set(new Date())
Expand Down Expand Up @@ -460,3 +462,26 @@ it('parse w, ww', () => {
const format2 = 'YYYY-[w]ww'
expect(dayjs(input2, format2).format(format1)).toBe(input2)
})

it('parse GGGG', () => {
const input1 = '2024'
const input2 = '2023'
const input3 = '2022'
const input4 = '2021'
const input5 = '2020'
const format = 'GGGG'
expect(dayjs(input1, format).valueOf()).toBe(moment(input1, format).valueOf())
expect(dayjs(input2, format).valueOf()).toBe(moment(input2, format).valueOf())
expect(dayjs(input3, format).valueOf()).toBe(moment(input3, format).valueOf())
expect(dayjs(input4, format).valueOf()).toBe(moment(input4, format).valueOf())
expect(dayjs(input5, format).valueOf()).toBe(moment(input5, format).valueOf())
})

it('parse W, WW', () => {
const input1 = '2024-W1'
const format1 = 'GGGG-[W]W'
expect(dayjs(input1, format1).valueOf()).toBe(moment(input1, format1).valueOf())
const input2 = '2024-W32'
const format2 = 'GGGG-[W]WW'
expect(dayjs(input2, format2).valueOf()).toBe(moment(input2, format2).valueOf())
})

0 comments on commit 15a18f3

Please sign in to comment.