Skip to content

Commit

Permalink
fix: Fix WeekOfYear Plugin wrong calender week number bug
Browse files Browse the repository at this point in the history
fix #760
  • Loading branch information
iamkun committed Dec 30, 2019
1 parent 66ce23f commit 79b86db
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 14 deletions.
17 changes: 3 additions & 14 deletions src/plugin/weekOfYear/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,14 @@ export default (o, c, d) => {
if (week !== null) {
return this.add((week - this.week()) * 7, 'day')
}

const weekStart = this.$locale().weekStart || 0

// d(this) clone is for badMutable plugin
const endOfYear = d(this).endOf(Y)
if (
weekStart === 0 &&
endOfYear.day() !== 6 &&
this.month() === 11 &&
31 - this.date() <= endOfYear.day()
) {
return 1
}

const startOfYear = d(this).startOf(Y)
const compareDay = startOfYear.subtract(startOfYear.day() - weekStart, D).subtract(1, MS)
const diffInWeek = this.diff(compareDay, W, true)
return Math.ceil(diffInWeek)
const result = Math.ceil(diffInWeek)
return result > 52 ? 1 : result
}

proto.weeks = function (week = null) {
return this.week(week)
}
Expand Down
1 change: 1 addition & 0 deletions test/plugin/weekOfYear.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ it('Week of year with locale', () => {
// Edges
expect(dayjs('2018-12-30').week()).toBe(moment('2018-12-30').week())
expect(dayjs('2019-12-29').week()).toBe(moment('2019-12-29').week())
expect(dayjs('2019-12-30').week()).toBe(moment('2019-12-30').week())
})

it('Format w ww wo', () => {
Expand Down

0 comments on commit 79b86db

Please sign in to comment.