Repository: iamkun/dayjs
Pull request #266 (2018-07-03 07:26:23)

update weekOfYear


Patch:
diff --git a/docs/en/API-reference.md b/docs/en/API-reference.md
index fb6bd2b..cd2051a 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 7538f5e..c814cf4 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 897493c..aaefb76 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 f2b8eca..67f89dc 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 504bb8e..c6f7bb3 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 0adc967..3b42fc5 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 b9f6c1c..c1fff90 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 e22f754..1739e0c 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 a83a9a6..9594946 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插件
diff --git a/src/plugin/weekOfYear/index.js b/src/plugin/weekOfYear/index.js
index 99a168e..0682bb4 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)
   }
 }


Test patch:
diff --git a/test/plugin/weekOfYear.test.js b/test/plugin/weekOfYear.test.js
index a99b4b7..443ecac 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())
 })