Skip to content

Commit fa03689

Browse files
committed
fix: fix week() error near the end of the year
1 parent 03bb181 commit fa03689

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

src/plugin/weekOfYear/index.js

+10-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
import { MILLISECONDS_A_DAY } from '../../constant'
1+
import { MS, Y, D, W } from '../../constant'
22

3-
export default (o, c) => {
3+
export default (o, c, d) => {
44
const proto = c.prototype
55
proto.week = function () {
6-
const day = this.$W || 7 // Return sunday as 7
7-
// Create date at nearest thursday
8-
const ins = new Date(this.$y, this.$M, (this.$D - day) + 4)
9-
const yearStart = new Date(Date.UTC(this.$y, 0, 1)) // Get first day of year
10-
return Math.ceil((((ins - yearStart) / MILLISECONDS_A_DAY) + 1) / 7) // Calculate weeks
6+
const endOfYear = this.endOf(Y)
7+
if (endOfYear.day() !== 6 && this.month() === 11 && (31 - this.date()) <= endOfYear.day()) {
8+
return 1
9+
}
10+
const startOfYear = d(this.$d).startOf(Y)
11+
const compareDay = startOfYear.subtract(startOfYear.day(), D).subtract(1, MS)
12+
const diffInWeek = this.diff(compareDay, W, true)
13+
return Math.ceil(diffInWeek)
1114
}
1215
}

test/plugin/weekOfYear.test.js

+2
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,7 @@ afterEach(() => {
1414
})
1515

1616
it('Week of year', () => {
17+
const day = '2018-12-31T10:59:09+08:00'
18+
expect(dayjs(day).week()).toBe(moment(day).week())
1719
expect(dayjs().week()).toBe(moment().week())
1820
})

0 commit comments

Comments
 (0)