customParseFormat: week token ('w'/'ww') is silently ignored when parsing with dayjs.utc()
Describe the bug
When parsing a date string that contains a week-of-year token (w or ww) using dayjs.utc(input, format) together with the customParseFormat plugin, the parsed week number is silently discarded and the resulting date defaults to week 1 of the year.
This is the same underlying issue that was fixed for non-UTC parsing in #2705 (released in 1.11.13), but the fix was only applied to the local-time branch of parseFormattedInput, not the UTC branch.
Looking at src/plugin/customParseFormat/index.js:
if (utc) {
return new Date(Date.UTC(y, M, d, h, m, s, ms)) // <-- returns before `week` is ever used
}
let newDate = new Date(y, M, d, h, m, s, ms)
if (week) {
newDate = dayjs(newDate).week(week).toDate() // <-- week is only applied here
}
return newDateThe if (utc) branch returns immediately and never checks the week variable, so any week number parsed from the input string is lost when using dayjs.utc().
To Reproduce
import dayjs from 'dayjs'
import utc from 'dayjs/plugin/utc'
import weekOfYear from 'dayjs/plugin/weekOfYear'
import customParseFormat from 'dayjs/plugin/customParseFormat'
dayjs.extend(utc)
dayjs.extend(weekOfYear)
dayjs.extend(customParseFormat)
// Local mode: works correctly (fixed in 1.11.13 / #2705)
console.log(dayjs('2024-31', 'YYYY-ww').format('YYYY-ww')) // 2024-31 ✅
// UTC mode: week is ignored
console.log(dayjs.utc('2024-31', 'YYYY-ww').format('YYYY-ww')) // 2024-01 ❌Expected behavior
dayjs.utc('2024-31', 'YYYY-ww').format('YYYY-ww') // should return "2024-31", matching the behavior already fixed for non-UTC parsing
Suggested fix
Apply the same week handling before branching on utc, e.g.:
let newDate = utc
? new Date(Date.UTC(y, M, d, h, m, s, ms))
: new Date(y, M, d, h, m, s, ms)
if (week) {
newDate = (utc ? dayjs.utc(newDate) : dayjs(newDate)).week(week).toDate()
}
return newDateInformation
- Day.js Version: 1.11.19 (latest, verified in source on
devbranch — the UTC branch has not been updated since the 1.11.13 fix) - Plugins used:
utc,weekOfYear,customParseFormat - Environment: Node.js / any (logic bug, not environment-specific)
Source: iamkun/dayjs