Skip to content

Commit

Permalink
Merge pull request #266 from iamkun/feature/iamkun
Browse files Browse the repository at this point in the history
update weekOfYear
  • Loading branch information
iamkun authored Jul 3, 2018
2 parents 052f4e7 + 1fa903c commit a83dfce
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 8 deletions.
7 changes: 7 additions & 0 deletions docs/en/API-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
7 changes: 7 additions & 0 deletions docs/ja/API-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Day.js は組み込みの `Date.prototype` を変更する代わりに `Dayjs`
* [Plugin APIs](#plugin-apis)
* [RelativeTime](#relativetime)
* [IsLeapYear](#isleapyear)
* [WeekOfYear](#weekofyear)

---

Expand Down Expand Up @@ -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)
11 changes: 11 additions & 0 deletions docs/ja/Plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 プラグインを構築することができます。
Expand Down
7 changes: 7 additions & 0 deletions docs/ko/API-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ Day.js는 네이티브 `Date.prototype`을 수정하는 대신 `Dayjs` 오브젝
- [Plugin APIs](#plugin-apis)
- [RelativeTime](#relativetime)
- [IsLeapYear](#isleapyear)
- [WeekOfYear](#weekofyear)

## Parsing

Expand Down Expand Up @@ -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)
11 changes: 11 additions & 0 deletions docs/ko/Plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 플러그인을 만들 수 있습니다.
Expand Down
7 changes: 7 additions & 0 deletions docs/pt-br/API-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
11 changes: 11 additions & 0 deletions docs/pt-br/Plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 8 additions & 1 deletion docs/zh-cn/API-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
* [插件 APIs](#plugin-apis)
* [相对时间](#relativetime)
* [是否是闰年](#是否是闰年)
* [年中的第几周](#年中的第几周)

---
如果没有特别说明,Day.js 的返回值都是新的 `Dayjs` 对象。
Expand Down Expand Up @@ -361,4 +362,10 @@ dayjs('2000-01-01').isLeapYear(); // true

`.isLeapYear` 获得是否闰年

插件 [`IsLeapYear`](./Plugin.md#isleapyear)
插件 [`IsLeapYear`](./Plugin.md#isleapyear)

### 年中的第几周

`.week` 获取是第几个周

插件 [`WeekOfYear`](./Plugin.md#weekofyear)
11 changes: 11 additions & 0 deletions docs/zh-cn/Plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -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插件
Expand Down
17 changes: 10 additions & 7 deletions src/plugin/weekOfYear/index.js
Original file line number Diff line number Diff line change
@@ -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)
}
}
2 changes: 2 additions & 0 deletions test/plugin/weekOfYear.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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())
})

0 comments on commit a83dfce

Please sign in to comment.