From 9007cc5ee66145c721bd2112e0546e38f652bac8 Mon Sep 17 00:00:00 2001 From: iamkun Date: Mon, 15 Apr 2019 12:59:18 +0800 Subject: [PATCH] fix: Add weekday (locale aware day of the week) plugin (#569) fix #559 --- docs/en/API-reference.md | 6 ++ docs/en/Plugin.md | 13 ++++ docs/es-es/API-reference.md | 6 ++ docs/es-es/Plugin.md | 13 ++++ docs/ja/API-reference.md | 116 +++++++++++++++++------------------- docs/ja/Plugin.md | 13 ++++ docs/ko/API-reference.md | 6 ++ docs/ko/Plugin.md | 13 ++++ docs/pt-br/API-reference.md | 6 ++ docs/pt-br/Plugin.md | 13 ++++ docs/zh-cn/API-reference.md | 6 ++ docs/zh-cn/Plugin.md | 13 ++++ src/plugin/weekday/index.js | 13 ++++ test/plugin/weekday.test.js | 43 +++++++++++++ types/plugin/weekday.d.ts | 12 ++++ 15 files changed, 231 insertions(+), 61 deletions(-) create mode 100644 src/plugin/weekday/index.js create mode 100644 test/plugin/weekday.test.js create mode 100644 types/plugin/weekday.d.ts diff --git a/docs/en/API-reference.md b/docs/en/API-reference.md index ee716b2d4..aa8ad550e 100644 --- a/docs/en/API-reference.md +++ b/docs/en/API-reference.md @@ -455,6 +455,12 @@ plugin [`IsLeapYear`](./Plugin.md#isleapyear) plugin [`WeekOfYear`](./Plugin.md#weekofyear) +### WeekDay + +`.weekday` to get or set locale aware day of the week + +plugin [`WeekDay`](./Plugin.md#weekday) + ### IsoWeeksInYear `.isoWeeksInYear` to get the number of weeks in year diff --git a/docs/en/Plugin.md b/docs/en/Plugin.md index c54ec91cd..506e6a992 100644 --- a/docs/en/Plugin.md +++ b/docs/en/Plugin.md @@ -286,6 +286,19 @@ dayjs('2018-06-27').week() // 26 dayjs('2018-06-27').week(5) // set week ``` +### WeekDay + +- WeekDay adds `.weekday()` API to get or set locale aware day of the week. + +```javascript +import weekDay from 'dayjs/plugin/weekDay' + +dayjs.extend(weekDay) +// when Monday is the first day of the week +dayjs().weekday(-7) // last Monday +dayjs().weekday(7) // next Monday +``` + ### IsoWeeksInYear - IsoWeeksInYear adds `.isoWeeksInYear()` API to return a `number` to get the number of weeks in year, according to ISO weeks. diff --git a/docs/es-es/API-reference.md b/docs/es-es/API-reference.md index 80b25ab02..01e397d75 100644 --- a/docs/es-es/API-reference.md +++ b/docs/es-es/API-reference.md @@ -455,6 +455,12 @@ complemento [`IsLeapYear`](./Plugin.md#isleapyear) complemento [`WeekOfYear`](./Plugin.md#weekofyear) +### WeekDay + +`.weekday` to get or set locale aware day of the week + +plugin [`WeekDay`](./Plugin.md#weekday) + ### IsoWeeksInYear `.isoWeeksInYear` to get the number of weeks in year diff --git a/docs/es-es/Plugin.md b/docs/es-es/Plugin.md index 1b5362afd..fe05f0806 100644 --- a/docs/es-es/Plugin.md +++ b/docs/es-es/Plugin.md @@ -282,6 +282,19 @@ dayjs('2018-06-27').week() // 26 dayjs('2018-06-27').week(5) // set week ``` +### WeekDay + +- WeekDay adds `.weekday()` API to get or set locale aware day of the week. + +```javascript +import weekDay from 'dayjs/plugin/weekDay' + +dayjs.extend(weekDay) +// when Monday is the first day of the week +dayjs().weekday(-7) // last Monday +dayjs().weekday(7) // next Monday +``` + ### IsoWeeksInYear - IsoWeeksInYear adds `.isoWeeksInYear()` API to return a `number` to get the number of weeks in year, according to ISO weeks. diff --git a/docs/ja/API-reference.md b/docs/ja/API-reference.md index 329be48e1..27d06a143 100644 --- a/docs/ja/API-reference.md +++ b/docs/ja/API-reference.md @@ -50,18 +50,6 @@ Day.js は組み込みの `Date.prototype` を変更する代わりに `Dayjs` - [Is a Dayjs `.isDayjs(compared: any)`](#is-a-dayjs-isdayjscompared-any) - [UTC](#utc) - [Plugin APIs](#plugin-apis) - - [RelativeTime](#relativetime) - - [IsLeapYear](#isleapyear) - - [WeekOfYear](#weekofyear) - - [IsoWeeksInYear](#isoweeksinyear) - - [IsSameOrAfter](#issameorafter) - - [IsSameOrBefore](#issameorbefore) - - [IsBetween](#isbetween) - - [QuarterOfYear](#quarterofyear) - - [ToArray](#toarray) - - [ToObject](#toobject) - - [MinMax](#minmax) - - [Calendar](#calendar) ## Parsing @@ -73,7 +61,7 @@ Day.js は組み込みの `Date.prototype` を変更する代わりに `Dayjs` dayjs() ``` -Day.jsは他の日付フォーマットもパースします。 +Day.js は他の日付フォーマットもパースします。 #### [ISO 8601](https://ja.wikipedia.org/wiki/ISO_8601) 形式 @@ -89,7 +77,7 @@ dayjs(new Date(2018, 8, 18)) #### Unix Timestamp (milliseconds) -Unixタイムスタンプ(Unixエポックのミリ秒)から`Dayjs`オブジェクトを返します。 +Unix タイムスタンプ(Unix エポックのミリ秒)から`Dayjs`オブジェクトを返します。 ```js dayjs(1318781876406) @@ -97,7 +85,7 @@ dayjs(1318781876406) ### Unix Timestamp (seconds) `.unix(value: number)` -Unixタイムスタンプ(Unixエポックの秒)から`Dayjs`オブジェクトを返します。 +Unix タイムスタンプ(Unix エポックの秒)から`Dayjs`オブジェクトを返します。 ```js dayjs.unix(1318781876) @@ -210,16 +198,16 @@ dayjs().get('day') #### List of all available units -| 単位 | ショートハンド | 説明 | -| ---- | ---- | ---- | -| `date` | | 月の日ひち | -| `day` | `d` | 曜日(日曜日は`0`、土曜日は`6`) | -| `month` | `M` | 月(1月は`0`、12月は`11`) | -| `year` | `y` | 年 | -| `hour` | `h` | 時 | -| `minute` | `m` | 分 | -| `second` | `s` | 秒 | -| `millisecond` | `ms` | ミリ秒 | +| 単位 | ショートハンド | 説明 | +| ------------- | -------------- | -------------------------------- | +| `date` | | 月の日ひち | +| `day` | `d` | 曜日(日曜日は`0`、土曜日は`6`) | +| `month` | `M` | 月(1 月は`0`、12 月は`11`) | +| `year` | `y` | 年 | +| `hour` | `h` | 時 | +| `minute` | `m` | 分 | +| `second` | `s` | 秒 | +| `millisecond` | `ms` | ミリ秒 | ### Set `.set(unit: string, value: number)` @@ -291,40 +279,40 @@ dayjs('2019-01-25').format('DD/MM/YYYY') // '25/01/2019' #### List of all available formats -| フォーマット | 出力 | 説明 | -| ------ | ---------------- | ------------------------------------- | -| `YY` | 18 | 2桁の年 | -| `YYYY` | 2018 | 4桁の年 | -| `M` | 1-12 | 1始まりの月 | -| `MM` | 01-12 | 1始まりの2桁の月 | -| `MMM` | Jan-Dec | 月の略称 | -| `MMMM` | January-December | 月の正式名 | -| `D` | 1-31 | 月ごとの日にち | -| `DD` | 01-31 | 月ごとの2桁の日にち | -| `d` | 0-6 | `0`で日曜日から始まる曜日 | -| `dd` | Su-Sa | 最も短い曜日の略称 | -| `ddd` | Sun-Sat | 曜日の略称 | -| `dddd` | Sunday-Saturday | 曜日名 | -| `H` | 0-23 | 時間 | -| `HH` | 00-23 | 2桁の時間 | -| `h` | 1-12 | 12時制の時間 | -| `hh` | 01-12 | 12時制で2桁の時間 | -| `m` | 0-59 | 分 | -| `mm` | 00-59 | 2桁の分 | -| `s` | 0-59 | 秒 | -| `ss` | 00-59 | 2桁の秒 | -| `SSS` | 000-999 | 3桁のミリ秒 | -| `Z` | +5:00 | UTCからのオフセット | -| `ZZ` | +0500 | UTCからの2桁のオフセット | -| `A` | AM PM | 午前と午後(大文字) | -| `a` | am pm | 午前と午後(小文字) | +| フォーマット | 出力 | 説明 | +| ------------ | ---------------- | --------------------------- | +| `YY` | 18 | 2 桁の年 | +| `YYYY` | 2018 | 4 桁の年 | +| `M` | 1-12 | 1 始まりの月 | +| `MM` | 01-12 | 1 始まりの 2 桁の月 | +| `MMM` | Jan-Dec | 月の略称 | +| `MMMM` | January-December | 月の正式名 | +| `D` | 1-31 | 月ごとの日にち | +| `DD` | 01-31 | 月ごとの 2 桁の日にち | +| `d` | 0-6 | `0`で日曜日から始まる曜日 | +| `dd` | Su-Sa | 最も短い曜日の略称 | +| `ddd` | Sun-Sat | 曜日の略称 | +| `dddd` | Sunday-Saturday | 曜日名 | +| `H` | 0-23 | 時間 | +| `HH` | 00-23 | 2 桁の時間 | +| `h` | 1-12 | 12 時制の時間 | +| `hh` | 01-12 | 12 時制で 2 桁の時間 | +| `m` | 0-59 | 分 | +| `mm` | 00-59 | 2 桁の分 | +| `s` | 0-59 | 秒 | +| `ss` | 00-59 | 2 桁の秒 | +| `SSS` | 000-999 | 3 桁のミリ秒 | +| `Z` | +5:00 | UTC からのオフセット | +| `ZZ` | +0500 | UTC からの 2 桁のオフセット | +| `A` | AM PM | 午前と午後(大文字) | +| `a` | am pm | 午前と午後(小文字) | - 利用可能な他のフォーマット `Q Do k kk X x ...` in plugin [`AdvancedFormat`](./Plugin.md#advancedformat) - ローカライズのフォーマットオプション `L LT LTS ...` in plugin [`LocalizedFormat`](./Plugin.md#localizedFormat) ### Difference `.diff(compared: Dayjs, unit: string (default: 'milliseconds'), float?: boolean)` -2つの`Dayjs`オブジェクトの差分を指定した単位で数値で返します。 +2 つの`Dayjs`オブジェクトの差分を指定した単位で数値で返します。 ```js const date1 = dayjs('2019-01-25') @@ -337,7 +325,7 @@ date1.diff(date2, 'day') // 233 ### Unix Timestamp (milliseconds) `.valueOf()` -`Dayjs`オブジェクトのUnixエポックからのミリ秒を数値で返します。 +`Dayjs`オブジェクトの Unix エポックからのミリ秒を数値で返します。 ```js dayjs('2019-01-25').valueOf() // 1548381600000 @@ -345,7 +333,7 @@ dayjs('2019-01-25').valueOf() // 1548381600000 ### Unix Timestamp (seconds) `.unix()` -`Dayjs`オブジェクトのUnixエポックからの秒を数値で返します。 +`Dayjs`オブジェクトの Unix エポックからの秒を数値で返します。 ```js dayjs('2019-01-25').unix() // 1548381600 @@ -353,7 +341,7 @@ dayjs('2019-01-25').unix() // 1548381600 ### UTC Offset (minutes) `.utcOffset()` -`Dayjs`オブジェクトのUTCオフセットを分単位の数値で返します。 +`Dayjs`オブジェクトの UTC オフセットを分単位の数値で返します。 ```js dayjs().utcOffset() @@ -377,7 +365,7 @@ dayjs('2019-01-25').toDate() ### As JSON `.toJSON()` -`Dayjs`オブジェクトの日付をISO8601形式にして文字列で返します。 +`Dayjs`オブジェクトの日付を ISO8601 形式にして文字列で返します。 ```js dayjs('2019-01-25').toJSON() // '2019-01-25T02:00:00.000Z' @@ -385,7 +373,7 @@ dayjs('2019-01-25').toJSON() // '2019-01-25T02:00:00.000Z' ### As ISO 8601 String `.toISOString()` -`Dayjs`オブジェクトの日付をISO8601形式にして文字列で返します。 +`Dayjs`オブジェクトの日付を ISO8601 形式にして文字列で返します。 ```js dayjs('2019-01-25').toISOString() // '2019-01-25T02:00:00.000Z' @@ -445,7 +433,7 @@ dayjs() instanceof dayjs // true ## UTC -UTCでパースや表示をしたい場合は、[`UTC`](./Plugin.md#utc)プラグインの`.utc` `.local` `.isUTC` で行えます。 +UTC でパースや表示をしたい場合は、[`UTC`](./Plugin.md#utc)プラグインの`.utc` `.local` `.isUTC` で行えます。 ## Plugin APIs @@ -467,6 +455,12 @@ UTCでパースや表示をしたい場合は、[`UTC`](./Plugin.md#utc)プラ プラグイン [`WeekOfYear`](./Plugin.md#weekofyear) +### WeekDay + +`.weekday` to get or set locale aware day of the week + +plugin [`WeekDay`](./Plugin.md#weekday) + ### IsoWeeksInYear `.isoWeeksInYear` でその年の週数が得られます。 @@ -487,7 +481,7 @@ UTCでパースや表示をしたい場合は、[`UTC`](./Plugin.md#utc)プラ ### IsBetween -`.isBetween`で他の2つの日付の間であるかどうかを得られます。 +`.isBetween`で他の 2 つの日付の間であるかどうかを得られます。 プラグイン [`IsBetween`](./Plugin.md#isbetween) @@ -519,4 +513,4 @@ UTCでパースや表示をしたい場合は、[`UTC`](./Plugin.md#utc)プラ `.calendar`で与えた日付のカレンダー上の情報が得られます。 -プラグイン [`Calendar`](./Plugin.md#calendar) \ No newline at end of file +プラグイン [`Calendar`](./Plugin.md#calendar) diff --git a/docs/ja/Plugin.md b/docs/ja/Plugin.md index 2daa02cb0..487674770 100644 --- a/docs/ja/Plugin.md +++ b/docs/ja/Plugin.md @@ -291,6 +291,19 @@ dayjs('06/27/2018').week() // 26 dayjs('2018-06-27').week(5) // set week ``` +### WeekDay + +- WeekDay adds `.weekday()` API to get or set locale aware day of the week. + +```javascript +import weekDay from 'dayjs/plugin/weekDay' + +dayjs.extend(weekDay) +// when Monday is the first day of the week +dayjs().weekday(-7) // last Monday +dayjs().weekday(7) // next Monday +``` + ### IsoWeeksInYear - IsoWeeksInYear adds `.isoWeeksInYear()` API to return a `number` to get the number of weeks in year, according to ISO weeks. diff --git a/docs/ko/API-reference.md b/docs/ko/API-reference.md index efe06d52b..c53303c41 100644 --- a/docs/ko/API-reference.md +++ b/docs/ko/API-reference.md @@ -454,6 +454,12 @@ plugin [`IsLeapYear`](./Plugin.md#isleapyear) plugin [`WeekOfYear`](./Plugin.md#weekofyear) +### WeekDay + +`.weekday` to get or set locale aware day of the week + +plugin [`WeekDay`](./Plugin.md#weekday) + ### IsoWeeksInYear `.isoWeeksInYear` to get the number of weeks in year diff --git a/docs/ko/Plugin.md b/docs/ko/Plugin.md index 25c706221..24e6f521e 100644 --- a/docs/ko/Plugin.md +++ b/docs/ko/Plugin.md @@ -283,6 +283,19 @@ dayjs('06/27/2018').week() // 26 dayjs('2018-06-27').week(5) // set week ``` +### WeekDay + +- WeekDay adds `.weekday()` API to get or set locale aware day of the week. + +```javascript +import weekDay from 'dayjs/plugin/weekDay' + +dayjs.extend(weekDay) +// when Monday is the first day of the week +dayjs().weekday(-7) // last Monday +dayjs().weekday(7) // next Monday +``` + ### IsoWeeksInYear - IsoWeeksInYear adds `.isoWeeksInYear()` API to return a `number` to get the number of weeks in year, according to ISO weeks. diff --git a/docs/pt-br/API-reference.md b/docs/pt-br/API-reference.md index e51c45ba0..9e3b8d9ab 100644 --- a/docs/pt-br/API-reference.md +++ b/docs/pt-br/API-reference.md @@ -453,6 +453,12 @@ plugin [`IsLeapYear`](./Plugin.md#isleapyear) plugin [`WeekOfYear`](./Plugin.md#weekofyear) +### WeekDay + +`.weekday` to get or set locale aware day of the week + +plugin [`WeekDay`](./Plugin.md#weekday) + ### IsoWeeksInYear `.isoWeeksInYear` to get the number of weeks in year diff --git a/docs/pt-br/Plugin.md b/docs/pt-br/Plugin.md index 676cff560..b264d51f5 100644 --- a/docs/pt-br/Plugin.md +++ b/docs/pt-br/Plugin.md @@ -282,6 +282,19 @@ dayjs('06/27/2018').week() // 26 dayjs('2018-06-27').week(5) // set week ``` +### WeekDay + +- WeekDay adds `.weekday()` API to get or set locale aware day of the week. + +```javascript +import weekDay from 'dayjs/plugin/weekDay' + +dayjs.extend(weekDay) +// when Monday is the first day of the week +dayjs().weekday(-7) // last Monday +dayjs().weekday(7) // next Monday +``` + ### IsoWeeksInYear - IsoWeeksInYear adds `.isoWeeksInYear()` API to return a `number` to get the number of weeks in year, according to ISO weeks. diff --git a/docs/zh-cn/API-reference.md b/docs/zh-cn/API-reference.md index 6df0de901..acc01f6c7 100644 --- a/docs/zh-cn/API-reference.md +++ b/docs/zh-cn/API-reference.md @@ -505,6 +505,12 @@ dayjs() instanceof dayjs // true 插件 [`WeekOfYear`](./Plugin.md#weekofyear) +### 星期 + +`.weekday` 来获取或设置当前语言的星期 + +plugin [`WeekDay`](./Plugin.md#weekday) + ### 年中有几周 ISO `.isoWeeksInYear` 获得年中有几周 diff --git a/docs/zh-cn/Plugin.md b/docs/zh-cn/Plugin.md index 6b82e2c3c..171388d42 100644 --- a/docs/zh-cn/Plugin.md +++ b/docs/zh-cn/Plugin.md @@ -285,6 +285,19 @@ dayjs('06/27/2018').week() // 26 dayjs('2018-06-27').week(5) // 设置周 ``` +### WeekDay + +- WeekDay 增加了 `.weekday()` API 来获取或设置当前语言的星期。 + +```javascript +import weekDay from 'dayjs/plugin/weekDay' + +dayjs.extend(weekDay) +// when Monday is the first day of the week +dayjs().weekday(-7) // last Monday +dayjs().weekday(7) // next Monday +``` + ### IsoWeeksInYear - IsoWeeksInYear 增加了 `.isoWeeksInYear()` API 返回一个 `number` 来得到依据 ISO week 标准一年中有几周 diff --git a/src/plugin/weekday/index.js b/src/plugin/weekday/index.js new file mode 100644 index 000000000..c3c86ef9b --- /dev/null +++ b/src/plugin/weekday/index.js @@ -0,0 +1,13 @@ +export default (o, c) => { + const proto = c.prototype + proto.weekday = function (input) { + const weekStart = this.$locale().weekStart || 0 + const { $W } = this + const weekday = ($W < weekStart ? $W + 7 : $W) - weekStart + if (this.$utils().u(input)) { + return weekday + } + return this.subtract(weekday, 'day').add(input, 'day') + } +} + diff --git a/test/plugin/weekday.test.js b/test/plugin/weekday.test.js new file mode 100644 index 000000000..703f3ec84 --- /dev/null +++ b/test/plugin/weekday.test.js @@ -0,0 +1,43 @@ +import MockDate from 'mockdate' +import moment from 'moment' +import dayjs from '../../src' +import weekday from '../../src/plugin/weekday' +import '../../src/locale/zh-cn' +import '../../src/locale/ar' + +dayjs.extend(weekday) + +beforeEach(() => { + MockDate.set(new Date()) +}) + +afterEach(() => { + MockDate.reset() + moment.locale('en') + dayjs.locale('en') +}) + +it('Sunday is the first day of the week', () => { + expect(dayjs().weekday()).toBe(moment().weekday()) + expect(dayjs().weekday(0).date()).toBe(moment().weekday(0).date()) + expect(dayjs().weekday(-7).format()).toBe(moment().weekday(-7).format()) + expect(dayjs().weekday(7).format()).toBe(moment().weekday(7).format()) +}) + +it('Monday is the first day of the week', () => { + moment.locale('zh-cn') + dayjs.locale('zh-cn') + expect(dayjs().weekday()).toBe(moment().weekday()) + expect(dayjs().weekday(0).date()).toBe(moment().weekday(0).date()) + expect(dayjs().weekday(-7).format()).toBe(moment().weekday(-7).format()) + expect(dayjs().weekday(7).format()).toBe(moment().weekday(7).format()) +}) + +it('Saturday is the first day of the week', () => { + moment.locale('ar') + dayjs.locale('ar') + expect(dayjs().weekday()).toBe(moment().weekday()) + expect(dayjs().weekday(0).date()).toBe(moment().weekday(0).date()) + expect(dayjs().weekday(-7).valueOf()).toBe(moment().weekday(-7).valueOf()) + expect(dayjs().weekday(7).valueOf()).toBe(moment().weekday(7).valueOf()) +}) diff --git a/types/plugin/weekday.d.ts b/types/plugin/weekday.d.ts new file mode 100644 index 000000000..87a8025a3 --- /dev/null +++ b/types/plugin/weekday.d.ts @@ -0,0 +1,12 @@ +import { PluginFunc } from 'dayjs' + +declare const plugin: PluginFunc +export = plugin + +declare module 'dayjs' { + interface Dayjs { + weekday(): number + + weekday(value: number): Dayjs + } +}