From 6fa3f88a019df8b5a1d140757938a8ab19ffefe2 Mon Sep 17 00:00:00 2001 From: iamkun Date: Tue, 10 Jul 2018 11:24:10 +0800 Subject: [PATCH 1/2] chore: update set logic --- src/index.js | 2 +- src/utils.js | 10 ---------- test/utils.test.js | 6 ------ 3 files changed, 1 insertion(+), 17 deletions(-) diff --git a/src/index.js b/src/index.js index 09729827e..e34a920ff 100644 --- a/src/index.js +++ b/src/index.js @@ -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) diff --git a/src/utils.js b/src/utils.js index a70a09d08..f993ae9e4 100644 --- a/src/utils.js +++ b/src/utils.js @@ -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, diff --git a/test/utils.test.js b/test/utils.test.js index b66f52e7a..5a4dbaa50 100644 --- a/test/utils.test.js +++ b/test/utils.test.js @@ -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) -}) From 27103f5aea3aae0275fef5eaf9f4faec4ae0a81c Mon Sep 17 00:00:00 2001 From: iamkun Date: Tue, 10 Jul 2018 15:15:44 +0800 Subject: [PATCH 2/2] docs: update set units docs --- docs/en/API-reference.md | 13 +++++++++++++ docs/ja/API-reference.md | 13 +++++++++++++ docs/ko/API-reference.md | 13 +++++++++++++ docs/pt-br/API-reference.md | 13 +++++++++++++ docs/zh-cn/API-reference.md | 14 ++++++++++++++ 5 files changed, 66 insertions(+) diff --git a/docs/en/API-reference.md b/docs/en/API-reference.md index 7c6581671..daba03a97 100644 --- a/docs/en/API-reference.md +++ b/docs/en/API-reference.md @@ -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. diff --git a/docs/ja/API-reference.md b/docs/ja/API-reference.md index ea142b03f..e7b496374 100644 --- a/docs/ja/API-reference.md +++ b/docs/ja/API-reference.md @@ -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 diff --git a/docs/ko/API-reference.md b/docs/ko/API-reference.md index feac5eabf..c7efa74e7 100644 --- a/docs/ko/API-reference.md +++ b/docs/ko/API-reference.md @@ -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` 오브젝트는 여러 방법으로 처리할 수 있습니다. diff --git a/docs/pt-br/API-reference.md b/docs/pt-br/API-reference.md index a2c3bec6c..a98bfdf9e 100644 --- a/docs/pt-br/API-reference.md +++ b/docs/pt-br/API-reference.md @@ -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 diff --git a/docs/zh-cn/API-reference.md b/docs/zh-cn/API-reference.md index 00cd32484..2a2d62b76 100644 --- a/docs/zh-cn/API-reference.md +++ b/docs/zh-cn/API-reference.md @@ -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` 对象如下增加减少之类的操作: