Skip to content

Commit

Permalink
feat: Added method to retrieve week of the year
Browse files Browse the repository at this point in the history
  • Loading branch information
naulacambra committed Jun 26, 2018
1 parent a05e55e commit e1c1b1c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 4 additions & 0 deletions test/get-set.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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())
})
Expand Down
2 changes: 2 additions & 0 deletions test/index.d.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ dayjs().year()

dayjs().month()

dayjs().week()

dayjs().date()

dayjs().day()
Expand Down

0 comments on commit e1c1b1c

Please sign in to comment.