diff --git a/src/index.js b/src/index.js index 626be49a5..246ec4636 100644 --- a/src/index.js +++ b/src/index.js @@ -120,6 +120,14 @@ class Dayjs { return this.$M } + week() { + 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) / 86400000) + 1) / 7) // Calculate weeks + } + day() { return this.$W } diff --git a/test/get-set.test.js b/test/get-set.test.js index db3ab75e4..55041559d 100644 --- a/test/get-set.test.js +++ b/test/get-set.test.js @@ -18,6 +18,10 @@ it('Month', () => { expect(dayjs().month()).toBe(moment().month()) }) +it('Week', () => { + expect(dayjs().week()).toBe(moment().week()) +}) + it('Day of Week', () => { expect(dayjs().day()).toBe(moment().day()) }) diff --git a/test/index.d.test.ts b/test/index.d.test.ts index d4a44844b..f729688bf 100644 --- a/test/index.d.test.ts +++ b/test/index.d.test.ts @@ -16,6 +16,8 @@ dayjs().year() dayjs().month() +dayjs().week() + dayjs().date() dayjs().day()