dayjs icon indicating copy to clipboard operation
dayjs copied to clipboard

After Update To 1.10.3, show errors "t.week is not a function"

Open illusi03 opened this issue 4 years ago • 12 comments

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)]

illusi03 avatar Jan 11 '21 05:01 illusi03

Screen Shot 2021-01-11 at 11 31 19

There the error.

I downgrade dayjs to 1.8.30, and remove dayjs installed, has solved

Thanks in advice ...

illusi03 avatar Jan 11 '21 05:01 illusi03

A reproduction demo or code, please?

iamkun avatar Jan 11 '21 05:01 iamkun

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.

mikerourke avatar Jan 12 '21 04:01 mikerourke

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

iamkun avatar Jan 12 '21 04:01 iamkun

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 🤷‍♀️

mikerourke avatar Jan 12 '21 07:01 mikerourke

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

iamkun avatar Jan 12 '21 07:01 iamkun

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 avatar May 04 '22 12:05 tukusejssirs

@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')) 

GagnDeep avatar May 06 '22 03:05 GagnDeep

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 avatar May 06 '22 10:05 tukusejssirs

@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

GagnDeep avatar May 07 '22 03:05 GagnDeep

@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.

🤦‍♂️ 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 avatar May 07 '22 06:05 tukusejssirs

@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! It worked as expected.

dawangong avatar Jun 23 '22 06:06 dawangong