Skip to content

Commit

Permalink
fix: update set week logic
Browse files Browse the repository at this point in the history
chore: update set week logic Merge pull request #276 from iamkun/feature/iamkun
  • Loading branch information
iamkun authored Jul 11, 2018
2 parents f483c66 + 27103f5 commit 60b6325
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 17 deletions.
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)
})

0 comments on commit 60b6325

Please sign in to comment.