From fa03689e7b3df7e6129c1cb3155b3bc0480c2747 Mon Sep 17 00:00:00 2001 From: iamkun Date: Tue, 3 Jul 2018 15:16:08 +0800 Subject: [PATCH 1/2] fix: fix week() error near the end of the year --- src/plugin/weekOfYear/index.js | 17 ++++++++++------- test/plugin/weekOfYear.test.js | 2 ++ 2 files changed, 12 insertions(+), 7 deletions(-) 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()) }) From 811c8174d762f69a61cb0abe44b25f11cb799ee8 Mon Sep 17 00:00:00 2001 From: iamkun Date: Tue, 3 Jul 2018 15:21:37 +0800 Subject: [PATCH 2/2] docs: update weekofyear doc --- docs/en/API-reference.md | 7 +++++++ docs/ja/API-reference.md | 7 +++++++ docs/ja/Plugin.md | 11 +++++++++++ docs/ko/API-reference.md | 7 +++++++ docs/ko/Plugin.md | 11 +++++++++++ docs/pt-br/API-reference.md | 7 +++++++ docs/pt-br/Plugin.md | 11 +++++++++++ docs/zh-cn/API-reference.md | 9 ++++++++- docs/zh-cn/Plugin.md | 11 +++++++++++ 9 files changed, 80 insertions(+), 1 deletion(-) diff --git a/docs/en/API-reference.md b/docs/en/API-reference.md index fb6bd2bcf..cd2051aa8 100644 --- a/docs/en/API-reference.md +++ b/docs/en/API-reference.md @@ -48,6 +48,7 @@ The `Dayjs` object is immutable, that is, all API operations that change the `Da - [Plugin APIs](#plugin-apis) - [RelativeTime](#relativetime) - [IsLeapYear](#isleapyear) + - [WeekOfYear](#weekofyear) ## Parsing @@ -411,3 +412,9 @@ plugin [`RelativeTime`](./Plugin.md#relativetime) `.isLeapYear` to get is a leap year or not plugin [`IsLeapYear`](./Plugin.md#isleapyear) + +### WeekOfYear + +`.week` to get week of the year + +plugin [`WeekOfYear`](./Plugin.md#weekofyear) diff --git a/docs/ja/API-reference.md b/docs/ja/API-reference.md index 7538f5ee1..c814cf4b8 100644 --- a/docs/ja/API-reference.md +++ b/docs/ja/API-reference.md @@ -46,6 +46,7 @@ Day.js は組み込みの `Date.prototype` を変更する代わりに `Dayjs` * [Plugin APIs](#plugin-apis) * [RelativeTime](#relativetime) * [IsLeapYear](#isleapyear) + * [WeekOfYear](#weekofyear) --- @@ -485,3 +486,9 @@ plugin [`RelativeTime`](./Plugin.md#relativetime) `.isLeapYear` to get is a leap year or not plugin [`IsLeapYear`](./Plugin.md#isleapyear) + +### WeekOfYear + +`.week` to get week of the year + +plugin [`WeekOfYear`](./Plugin.md#weekofyear) diff --git a/docs/ja/Plugin.md b/docs/ja/Plugin.md index 897493c36..aaefb769b 100644 --- a/docs/ja/Plugin.md +++ b/docs/ja/Plugin.md @@ -145,6 +145,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 +``` + ## カスタマイズ さまざまなニーズに合わせて独自の Day.js プラグインを構築することができます。 diff --git a/docs/ko/API-reference.md b/docs/ko/API-reference.md index f2b8eca22..67f89dc52 100644 --- a/docs/ko/API-reference.md +++ b/docs/ko/API-reference.md @@ -48,6 +48,7 @@ Day.js는 네이티브 `Date.prototype`을 수정하는 대신 `Dayjs` 오브젝 - [Plugin APIs](#plugin-apis) - [RelativeTime](#relativetime) - [IsLeapYear](#isleapyear) + - [WeekOfYear](#weekofyear) ## Parsing @@ -411,3 +412,9 @@ dayjs('2000-01-01').isLeapYear(); // true `.isLeapYear` to get is a leap year or not plugin [`IsLeapYear`](./Plugin.md#isleapyear) + +### WeekOfYear + +`.week` to get week of the year + +plugin [`WeekOfYear`](./Plugin.md#weekofyear) diff --git a/docs/ko/Plugin.md b/docs/ko/Plugin.md index 504bb8ef6..c6f7bb3c8 100644 --- a/docs/ko/Plugin.md +++ b/docs/ko/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 다양한 요구를 충족하기위해 자신만의 Day.js 플러그인을 만들 수 있습니다. diff --git a/docs/pt-br/API-reference.md b/docs/pt-br/API-reference.md index 0adc967f3..3b42fc525 100644 --- a/docs/pt-br/API-reference.md +++ b/docs/pt-br/API-reference.md @@ -46,6 +46,7 @@ Este objeto `Dayjs` é imutável, ou seja, todas as operações desta API irão * [Plugin APIs](#plugin-apis) * [RelativeTime](#relativetime) * [IsLeapYear](#isleapyear) + * [WeekOfYear](#weekofyear) --- O Day.js sempre irá retornar um novo objeto `Dayjs` se nada for especificado. @@ -463,3 +464,9 @@ plugin [`RelativeTime`](./Plugin.md#relativetime) `.isLeapYear` to get is a leap year or not plugin [`IsLeapYear`](./Plugin.md#isleapyear) + +### WeekOfYear + +`.week` to get week of the year + +plugin [`WeekOfYear`](./Plugin.md#weekofyear) diff --git a/docs/pt-br/Plugin.md b/docs/pt-br/Plugin.md index b9f6c1cd1..c1fff9045 100644 --- a/docs/pt-br/Plugin.md +++ b/docs/pt-br/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 +``` + ## Customizar Você também pode construir seu próprio plugin Day.js para diferentes necessidades. Sinta-se à vontade para abrir um pull request e compartilhar seu plugin com a comunidade. diff --git a/docs/zh-cn/API-reference.md b/docs/zh-cn/API-reference.md index e22f754e5..1739e0c57 100644 --- a/docs/zh-cn/API-reference.md +++ b/docs/zh-cn/API-reference.md @@ -46,6 +46,7 @@ * [插件 APIs](#plugin-apis) * [相对时间](#relativetime) * [是否是闰年](#是否是闰年) + * [年中的第几周](#年中的第几周) --- 如果没有特别说明,Day.js 的返回值都是新的 `Dayjs` 对象。 @@ -361,4 +362,10 @@ dayjs('2000-01-01').isLeapYear(); // true `.isLeapYear` 获得是否闰年 -插件 [`IsLeapYear`](./Plugin.md#isleapyear) \ No newline at end of file +插件 [`IsLeapYear`](./Plugin.md#isleapyear) + +### 年中的第几周 + +`.week` 获取是第几个周 + +插件 [`WeekOfYear`](./Plugin.md#weekofyear) \ No newline at end of file diff --git a/docs/zh-cn/Plugin.md b/docs/zh-cn/Plugin.md index a83a9a6a9..959494674 100644 --- a/docs/zh-cn/Plugin.md +++ b/docs/zh-cn/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 增加了 `.week()` API 返回一个 `number` 来表示 `Dayjs` 的日期是年中第几周. + +```javascript +import weekOfYear from 'dayjs/plugin/weekOfYear' + +dayjs.extend(weekOfYear) + +dayjs('06/27/2018').week() // 26 +``` + ## 自定义 你可以根据需要自由的编写一个Day.js插件