Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: update set week logic #276

Merged
merged 2 commits into from
Jul 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions docs/en/API-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,19 @@ dayjs().set('month', 3); // April
dayjs().set('second', 30);
```

#### List of all available units

| Unit | Shorthand | Description |
| ------------- | --------- | ---------------------------------------- |
| `date` | | Date of Month |
| `day` | `d` | Day of Week (Sunday as 0, Saturday as 6) |
| `month` | `M` | Month |
| `year` | `y` | Year |
| `hour` | `h` | Hour |
| `minute` | `m` | Minute |
| `second` | `s` | Second |
| `millisecond` | `ms` | Millisecond |

## Manipulating

`Dayjs` objects can be manipulated in many ways.
Expand Down
13 changes: 13 additions & 0 deletions docs/ja/API-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,19 @@ dayjs().set('month', 3); // 4月
dayjs().set('second', 30);
```

#### List of all available units

| Unit | Shorthand | Description |
| ------------- | --------- | ---------------------------------------- |
| `date` | | Date of Month |
| `day` | `d` | Day of Week (Sunday as 0, Saturday as 6) |
| `month` | `M` | Month |
| `year` | `y` | Year |
| `hour` | `h` | Hour |
| `minute` | `m` | Minute |
| `second` | `s` | Second |
| `millisecond` | `ms` | Millisecond |

---

### Manipulate
Expand Down
13 changes: 13 additions & 0 deletions docs/ko/API-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,19 @@ dayjs().set('month', 3); // April
dayjs().set('second', 30);
```

#### List of all available units

| Unit | Shorthand | Description |
| ------------- | --------- | ---------------------------------------- |
| `date` | | Date of Month |
| `day` | `d` | Day of Week (Sunday as 0, Saturday as 6) |
| `month` | `M` | Month |
| `year` | `y` | Year |
| `hour` | `h` | Hour |
| `minute` | `m` | Minute |
| `second` | `s` | Second |
| `millisecond` | `ms` | Millisecond |

## Manipulating

`Dayjs` 오브젝트는 여러 방법으로 처리할 수 있습니다.
Expand Down
13 changes: 13 additions & 0 deletions docs/pt-br/API-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,19 @@ dayjs().set('month', 3); // April
dayjs().set('second', 30);
```

#### List of all available units

| Unit | Shorthand | Description |
| ------------- | --------- | ---------------------------------------- |
| `date` | | Date of Month |
| `day` | `d` | Day of Week (Sunday as 0, Saturday as 6) |
| `month` | `M` | Month |
| `year` | `y` | Year |
| `hour` | `h` | Hour |
| `minute` | `m` | Minute |
| `second` | `s` | Second |
| `millisecond` | `ms` | Millisecond |

---

### Manipulação
Expand Down
14 changes: 14 additions & 0 deletions docs/zh-cn/API-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,20 @@ dayjs().set('date', 1);
dayjs().set('month', 3); // 四月
dayjs().set('second', 30);
```

#### 可用单位

| 单位 | 缩写 | 描述 |
| ------------- | ---- | --------------------------- |
| `date` | | 日期 |
| `day` | `d` | 星期几 (星期天 0, 星期六 6) |
| `month` | `M` | 月 |
| `year` | `y` | 年 |
| `hour` | `h` | 时 |
| `minute` | `m` | 分 |
| `second` | `s` | 秒 |
| `millisecond` | `ms` | 毫秒 |

---
### 操作
您可以对 `Dayjs` 对象如下增加减少之类的操作:
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ class Dayjs {
const unit = Utils.prettyUnit(units)
switch (unit) {
case C.D:
Utils.setDay(this.$d, int)
this.$d.setDate(this.$D + (int - this.$W))
break
case C.DATE:
this.$d.setDate(int)
Expand Down
10 changes: 0 additions & 10 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,7 @@ const prettyUnit = (u) => {

const isUndefined = s => s === undefined

const setDay = (date, day) => {
const currentDay = date.getDay()
const distance = day - currentDay

date.setDate(date.getDate() + distance)

return date
}

export default {
setDay,
padStart,
padZoneStr,
monthDiff,
Expand Down
6 changes: 0 additions & 6 deletions test/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,3 @@ it('PadStart', () => {
expect(padStart(1, 2, '0')).toBe('01')
expect(padStart(0, 2, '0')).toBe('00')
})

it('SetDate', () => {
const day = Math.floor((Math.random() * 7) % 6)

expect(Utils.setDay((new Date()), day).getDay()).toBe(day)
})