diff --git a/src/plugin/weekOfYear/index.js b/src/plugin/weekOfYear/index.js index 99a168ed9..0682bb46d 100644 --- a/src/plugin/weekOfYear/index.js +++ b/src/plugin/weekOfYear/index.js @@ -1,12 +1,15 @@ -import { MILLISECONDS_A_DAY } from '../../constant' +import { MS, Y, D, W } from '../../constant' -export default (o, c) => { +export default (o, c, d) => { const proto = c.prototype proto.week = function () { - const day = this.$W || 7 // Return sunday as 7 - // Create date at nearest thursday - const ins = new Date(this.$y, this.$M, (this.$D - day) + 4) - const yearStart = new Date(Date.UTC(this.$y, 0, 1)) // Get first day of year - return Math.ceil((((ins - yearStart) / MILLISECONDS_A_DAY) + 1) / 7) // Calculate weeks + const endOfYear = this.endOf(Y) + if (endOfYear.day() !== 6 && this.month() === 11 && (31 - this.date()) <= endOfYear.day()) { + return 1 + } + const startOfYear = d(this.$d).startOf(Y) + const compareDay = startOfYear.subtract(startOfYear.day(), D).subtract(1, MS) + const diffInWeek = this.diff(compareDay, W, true) + return Math.ceil(diffInWeek) } } diff --git a/test/plugin/weekOfYear.test.js b/test/plugin/weekOfYear.test.js index a99b4b74d..443ecac98 100644 --- a/test/plugin/weekOfYear.test.js +++ b/test/plugin/weekOfYear.test.js @@ -14,5 +14,7 @@ afterEach(() => { }) it('Week of year', () => { + const day = '2018-12-31T10:59:09+08:00' + expect(dayjs(day).week()).toBe(moment(day).week()) expect(dayjs().week()).toBe(moment().week()) })