dayjs
dayjs copied to clipboard
After Update To 1.10.3, show errors "t.week is not a function"
Describe the bug Show error, t.week is not a function. After upgrade to 1.10.3
Expected behavior I expected this package No bugs :)
Information
- Day.js Version [v.1.10.3]
- OS: [windows 10, 64bit]
- Browser [chrome]
- Time zone: [GMT 08:00 DST (Pacific Daylight Time)]

There the error.
I downgrade dayjs to 1.8.30, and remove dayjs installed, has solved
Thanks in advice ...
A reproduction demo or code, please?
FYI, @illusi03, I ran into this issue as well. The workaround is to import/use the isoWeek
plugin:
import isoWeek from "dayjs/plugin/isoWeek";
//...
dayjs.extend(isoWeek);
I think adding a check for whether the .isoWeek()
method is available on this
should fix it (https://github.com/iamkun/dayjs/blob/dev/src/plugin/advancedFormat/index.js#L33). I can try putting a PR in with a fix tomorrow unless someone else beats me to it.
Can you please confirm how did you use this plugin? Have you format in 'WW'
or W
like
dayjs.extend(advancedFormat)
dayjs().format('WW W')
if so, isoWeek is needed and marked on the document https://day.js.org/docs/en/plugin/advanced-format
I needed to change the format of a date to adhere to the HTML week input, which is yyyy-Www
, where the uppercase W
is the actual letter "W", so I had to escape it with [W]
.
I don't actually use the isoWeek
plugin, I use the weekOfYear
plugin to get/set week numbers. I just added isoWeek
to get around the error. I'm not sure if that answers your question 🤷♀️
Well, that's the main reason. You should escape is with [W]
to get W
string, or use isoWeek plugin to format('W')
according to doc
I have the same problem, however, I need ww
to be the ISO week number. Tested on [email protected]
.
MWE:
const dayjs = require('dayjs')
const advancedFormat = require('dayjs/plugin/advancedFormat')
const utc = require('dayjs/plugin/utc')
const timezone = require('dayjs/plugin/timezone')
const isoWeek = require('dayjs/plugin/isoWeek')
dayjs.extend(advancedFormat)
dayjs.extend(utc)
dayjs.extend(timezone)
dayjs.extend(isoWeek)
dayjs.tz.setDefault('Europe/Bratislava')
const now = dayjs()
// console.log(`w :`, now.format('w')) // Does not work
console.log(`ww :`, now.format('ww')) // Does not work
Update: Is the order of extend()
functions important?
@tukusejssirs weekOfYear plugin solves this issue. Try importing this plugin before using any week based formatting from advancedFormat
plugin
import weekOfYear from "dayjs/plugin/weekOfYear";
import advancedFormat from "dayjs/plugin/advancedFormat"
dayjs.extend(weekOfYear)
dayjs.extend(advancedFormat)
const now = dayjs()
console.log(`ww :`, now.format('ww'))
Thanks, @GagnDeep, for helping me out! :wink: It indeed works as expected.
However, the docs are not stating the dependency on WeekOfYear
. They actually state that it depends on IsoWeek
.
I think the docs should provides use plugin dependency graph which would inform users of each and every plugin required by a particular plugin. And also state the order of extending dayjs
if that is important.
Of course, this is a bit OT topic to this issue.
Update: Actually, it does not work as I want ISO week number, not the other one (regular/American one). Current ISO week number is 18
, while WeekOfYear
outputs 19
.
Update 2
You can search the web that the current ISO week number is 18.
Updated MWE:
const dayjs = require('dayjs')
const advancedFormat = require('dayjs/plugin/advancedFormat')
const utc = require('dayjs/plugin/utc')
const timezone = require('dayjs/plugin/timezone')
const isoWeek = require('dayjs/plugin/isoWeek')
const weekOfYear = require('dayjs/plugin/weekOfYear')
dayjs.extend(advancedFormat)
dayjs.extend(utc)
dayjs.extend(timezone)
dayjs.extend(weekOfYear)
dayjs.extend(isoWeek)
dayjs.tz.setDefault('Europe/Bratislava')
const now = dayjs('2022-05-06')
console.log(`dayjs :`, now.format('ww')) // Output: 19
console.log(`iso :`, getWeekNumber(now)) // Output: 18
// Note: Based on https://weeknumber.com/how-to/javascript
function getWeekNumber(date) {
date = new Date(date)
date.setHours(0, 0, 0, 0)
// Thursday in current week decides the year.
date.setDate(date.getDate() + 3 - (date.getDay() + 6) % 7)
// January 4 is always in week 1.
var week1 = new Date(date.getFullYear(), 0, 4)
// Adjust to Thursday in week 1 and count number of weeks from date to week1.
return 1 + Math.round(((date.getTime() - week1.getTime()) / 86400000 - 3 + (week1.getDay() + 6) % 7) / 7)
}
@tukusejssirs To get iso week number you need to use WW
instead of ww
. It's in the table of accepted formats for advancedFormat plugin.
const dayjs = require('dayjs')
const advancedFormat = require('dayjs/plugin/advancedFormat')
const isoWeek = require('dayjs/plugin/isoWeek')
const weekOfYear = require('dayjs/plugin/weekOfYear')
dayjs.extend(advancedFormat)
dayjs.extend(weekOfYear)
dayjs.extend(isoWeek)
console.log(`dayjs :`, now.format('ww')) // Output: 19
console.log(`iso :`, now.format("WW")) // Output: 18
And you can also skip weekOfYear
plugin if you just need to get iso week
@tukusejssirs To get iso week number you need to use
WW
instead ofww
. It's in the table of accepted formats for advancedFormat plugin.
🤦♂️ Argh, you’re right I’ve read the table, but somehow missed the casing of W
.
I’m sorry for the noise and thank you for your patience and help! :pray:
@tukusejssirs weekOfYear plugin solves this issue. Try importing this plugin before using any week based formatting from
advancedFormat
pluginimport weekOfYear from "dayjs/plugin/weekOfYear"; import advancedFormat from "dayjs/plugin/advancedFormat" dayjs.extend(weekOfYear) dayjs.extend(advancedFormat) const now = dayjs() console.log(`ww :`, now.format('ww'))
Thanks, @GagnDeep, for helping me out! It worked as expected.