From e1c1b1cffd9c6db8aa02f7518559b51c82b67ec1 Mon Sep 17 00:00:00 2001 From: Arnau Lacambra <naulacambra@gmail.com> Date: Tue, 26 Jun 2018 16:52:46 +0200 Subject: [PATCH 1/3] feat: Added method to retrieve week of the year --- src/index.js | 8 ++++++++ test/get-set.test.js | 4 ++++ test/index.d.test.ts | 2 ++ 3 files changed, 14 insertions(+) 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() From f0cc18fa5ff4c38eb74c3ae5263ed60bec157033 Mon Sep 17 00:00:00 2001 From: Arnau Lacambra <naulacambra@gmail.com> Date: Wed, 27 Jun 2018 12:46:41 +0200 Subject: [PATCH 2/3] Moved week() from core to plugin --- src/index.js | 8 -------- src/plugin/weekOfYear/index.js | 12 ++++++++++++ test/get-set.test.js | 4 ---- test/index.d.test.ts | 2 -- test/plugin/weekOfYear.test.js | 18 ++++++++++++++++++ 5 files changed, 30 insertions(+), 14 deletions(-) create mode 100644 src/plugin/weekOfYear/index.js create mode 100644 test/plugin/weekOfYear.test.js diff --git a/src/index.js b/src/index.js index 246ec4636..626be49a5 100644 --- a/src/index.js +++ b/src/index.js @@ -120,14 +120,6 @@ 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/src/plugin/weekOfYear/index.js b/src/plugin/weekOfYear/index.js new file mode 100644 index 000000000..99a168ed9 --- /dev/null +++ b/src/plugin/weekOfYear/index.js @@ -0,0 +1,12 @@ +import { MILLISECONDS_A_DAY } from '../../constant' + +export default (o, c) => { + 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 + } +} diff --git a/test/get-set.test.js b/test/get-set.test.js index 55041559d..db3ab75e4 100644 --- a/test/get-set.test.js +++ b/test/get-set.test.js @@ -18,10 +18,6 @@ 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 f729688bf..d4a44844b 100644 --- a/test/index.d.test.ts +++ b/test/index.d.test.ts @@ -16,8 +16,6 @@ dayjs().year() dayjs().month() -dayjs().week() - dayjs().date() dayjs().day() diff --git a/test/plugin/weekOfYear.test.js b/test/plugin/weekOfYear.test.js new file mode 100644 index 000000000..a99b4b74d --- /dev/null +++ b/test/plugin/weekOfYear.test.js @@ -0,0 +1,18 @@ +import moment from 'moment' +import MockDate from 'mockdate' +import dayjs from '../../src' +import weekOfYear from '../../src/plugin/weekOfYear' + +dayjs.extend(weekOfYear) + +beforeEach(() => { + MockDate.set(new Date()) +}) + +afterEach(() => { + MockDate.reset() +}) + +it('Week of year', () => { + expect(dayjs().week()).toBe(moment().week()) +}) From 0731ebc5c2195100da9e25a5349563b077119ee7 Mon Sep 17 00:00:00 2001 From: Arnau Lacambra <naulacambra@gmail.com> Date: Wed, 27 Jun 2018 13:04:34 +0200 Subject: [PATCH 3/3] Updated Plugin.md doc file --- docs/en/Plugin.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/docs/en/Plugin.md b/docs/en/Plugin.md index 310bceab0..0d2ff590b 100644 --- a/docs/en/Plugin.md +++ b/docs/en/Plugin.md @@ -144,6 +144,17 @@ List of added formats: | `BBBB` | 2561 | Full BE Year (Year + 543) | | `BB` | 61 | 2-digit of BE Year | +### WeekOfYear + - WeekOfYear adds `.week()` API to returns a `number` indicating the `Dayjs`'s week of the year. + +```javascript +import weekOfYear from 'dayjs/plugin/weekOfYear' + +dayjs.extend(weekOfYear) + +dayjs('06/27/2018').week() // 26 +``` + ## Customize You could build your own Day.js plugin to meet different needs.