diff --git a/README.md b/README.md index fb91e5cfb..399dd4a42 100644 --- a/README.md +++ b/README.md @@ -86,9 +86,9 @@ dayjs('2018-05-05').locale('zh-cn').format() // use Chinese Simplified locale in A plugin is an independent module that can be added to Day.js to extend functionality or add new features. ```javascript -import AdvancedFormat from 'dayjs/plugin/AdvancedFormat' // load on demand +import advancedFormat from 'dayjs/plugin/advancedFormat' // load on demand -dayjs.extend(AdvancedFormat) // use plugin +dayjs.extend(advancedFormat) // use plugin dayjs().format('Q Do k kk X x') // more available formats ``` diff --git a/build/index.js b/build/index.js index 65b5bc75e..a5e08fda6 100644 --- a/build/index.js +++ b/build/index.js @@ -29,8 +29,8 @@ async function build(option) { const plugins = await promisifyReadDir(path.join(__dirname, '../src/plugin')) plugins.forEach((l) => { build(configFactory({ - input: `./src/plugin/${l}`, - fileName: `./plugin/${l}`, + input: `./src/plugin/${l}/index`, + fileName: `./plugin/${l}.js`, name: `dayjs_plugin_${formatName(l)}` })) }) diff --git a/docs/en/API-reference.md b/docs/en/API-reference.md index f896be9ae..a5420977f 100644 --- a/docs/en/API-reference.md +++ b/docs/en/API-reference.md @@ -45,6 +45,8 @@ The `Dayjs` object is immutable, that is, all API operations that change the `Da - [Is Same `.isSame(compared: Dayjs)`](#is-same-issamecompared-dayjs) - [Is After `.isAfter(compared: Dayjs)`](#is-after-isaftercompared-dayjs) - [Is Leap Year `.isLeapYear()`](#is-leap-year-isleapyear) + - [Plugin APIs](#plugin-apis) + - [RelativeTime](#relativetime) ## Parsing @@ -254,6 +256,8 @@ dayjs('2019-01-25').format('DD/MM/YYYY'); // '25/01/2019' | `A` | AM PM | | | `a` | am pm | | +* More available formats `Q Do k kk X x ...` in plugin [`AdvancedFormat`](./Plugin.md#advancedformat) + ### Difference `.diff(compared: Dayjs, unit: string (default: 'milliseconds'), float?: boolean)` Returns a `number` indicating the difference of two `Dayjs`s in the specified unit. @@ -379,3 +383,11 @@ Returns a `boolean` indicating whether the `Dayjs`'s year is a leap year or not. ```js dayjs('2000-01-01').isLeapYear(); // true ``` + +## Plugin APIs + +### RelativeTime + +`.from` `.to` `.fromNow` `.toNow` to get relative time + +plugin [`RelativeTime`](./Plugin.md#relativetime) diff --git a/docs/en/I18n.md b/docs/en/I18n.md index 50b7d275c..fe440702e 100644 --- a/docs/en/I18n.md +++ b/docs/en/I18n.md @@ -41,15 +41,6 @@ dayjs().locale('es').format() // use loaded locale locally dayjs('2018-4-28', { locale: es }) // through constructor ``` -#### Changing locales in API call - -Check API documents for more details. - -```js -import 'dayjs/locale/es' -dayjs('2018-4-28').format(format, es) // use locale in this API call only -``` - ## Installation * Via NPM: @@ -88,7 +79,22 @@ const localeObject = { name: 'es', // name String weekdays: 'Domingo_Lunes ...'.split('_'), // weekdays Array months: 'Enero_Febrero ... '.split('_'), // months Array - ordinal: n => `${n}º` // ordinal Function (number) => return number + output + ordinal: n => `${n}º`, // ordinal Function (number) => return number + output + relativeTime = { // relative time format strings, keep %s %d as the same + future: 'in %s', // e.g. in 2 hours, %s been replaced with 2hours + past: '%s ago', + s: 'a few seconds', + m: 'a minute', + mm: '%d minutes', + h: 'an hour', + hh: '%d hours', // e.g. 2 hours, %d been replaced with 2 + d: 'a day', + dd: '%d days', + M: 'a month', + MM: '%d months', + y: 'a year', + yy: '%d years' + } } ``` diff --git a/docs/en/Plugin.md b/docs/en/Plugin.md index 66e7430ed..c76f33182 100644 --- a/docs/en/Plugin.md +++ b/docs/en/Plugin.md @@ -47,9 +47,9 @@ dayjs.extend(AdvancedFormat) // use plugin - AdvancedFormat extends `dayjs().format` API to supply more format options. ```javascript -import AdvancedFormat from 'dayjs/plugin/AdvancedFormat' +import advancedFormat from 'dayjs/plugin/advancedFormat' -dayjs.extend(AdvancedFormat) +dayjs.extend(advancedFormat) dayjs().format('Q Do k kk X x') ``` @@ -65,6 +65,55 @@ List of added formats: | `X` | 1360013296 | Unix Timestamp in second | | `x` | 1360013296123 | Unix Timestamp in millisecond | +### RelativeTime + - RelativeTime adds `.from` `.to` `.fromNow` `.toNow` APIs to formats date to relative time strings (e.g. 3 hours ago). + +```javascript +import relativeTime from 'dayjs/plugin/relativeTime' + +dayjs.extend(relativeTime) + +dayjs().from(dayjs('1990')) // 2 years ago +dayjs().from(dayjs(), true) // 2 years + +dayjs().fromNow() + +dayjs().to(dayjs()) + +dayjs().toNow() +``` + +#### Time from now `.fromNow(withoutSuffix?: boolean)` + +Returns the `string` of relative time from now. + +#### Time from X `.from(compared: Dayjs, withoutSuffix?: boolean)` + +Returns the `string` of relative time from X. + +#### Time to now `.toNow(withoutSuffix?: boolean)` + +Returns the `string` of relative time to now. + +#### Time to X `.to(compared: Dayjs, withoutSuffix?: boolean)` + +Returns the `string` of relative time to X. + +| Range | Key | Sample Output | +| ------------------------ | ---- | -------------------------------- | +| 0 to 44 seconds | s | a few seconds ago | +| 45 to 89 seconds | m | a minute ago | +| 90 seconds to 44 minutes | mm | 2 minutes ago ... 44 minutes ago | +| 45 to 89 minutes | h | an hour ago | +| 90 minutes to 21 hours | hh | 2 hours ago ... 21 hours ago | +| 22 to 35 hours | d | a day ago | +| 36 hours to 25 days | dd | 2 days ago ... 25 days ago | +| 26 to 45 days | M | a month ago | +| 46 days to 10 months | MM | 2 months ago ... 10 months ago | +| 11 months to 17months | y | a year ago | +| 18 months+ | yy | 2 years ago ... 20 years ago | + + ## Customize You could build your own Day.js plugin to meet different needs. diff --git a/docs/ja/API-reference.md b/docs/ja/API-reference.md index 6f16f3446..f86061e87 100644 --- a/docs/ja/API-reference.md +++ b/docs/ja/API-reference.md @@ -43,6 +43,8 @@ Day.js は組み込みの `Date.prototype` を変更する代わりに `Dayjs` * [Is Same](#is-same) * [Is After](#is-after) * [Is Leap Year](#is-leap-year) +* [Plugin APIs](#plugin-apis) + * [RelativeTime](#relativetime) --- @@ -303,6 +305,8 @@ dayjs().format('{YYYY} MM-DDTHH:mm:ssZ[Z]'); // "{2014} 09-08T08:02:17-05:00Z" | `A` | AM PM | 午前と午後 (大文字) | | `a` | am pm | 午前と午後 (小文字) | +* More available formats `Q Do k kk X x ...` in plugin [`AdvancedFormat`](./Plugin.md#advancedformat) + #### Difference * Number を返します @@ -452,3 +456,11 @@ dayjs().isAfter(dayjs()); // false dayjs().isLeapYear(); dayjs('2000-01-01').isLeapYear(); // true ``` + +## Plugin APIs + +### RelativeTime + +`.from` `.to` `.fromNow` `.toNow` to get relative time + +plugin [`RelativeTime`](./Plugin.md#relativetime) diff --git a/docs/ja/I18n.md b/docs/ja/I18n.md index 968ab63b3..bbc0eff33 100644 --- a/docs/ja/I18n.md +++ b/docs/ja/I18n.md @@ -41,15 +41,6 @@ dayjs().locale('es').format() // 読み込んだロケールを特定のイン dayjs('2018-4-28', { locale: es } // コンストラクタを通して適用 ``` -#### API 呼び出し時のロケールの変更 - -詳細については API ドキュメントをご覧ください。 - -```js -import 'dayjs/locale/es' -dayjs('2018-4-28').format(format, es) // この API 呼び出しにのみロケールを適用 -``` - ## インストール * NPM を使う場合: @@ -90,7 +81,22 @@ const localeObject = { name: 'es', // ロケール名を表す文字列 weekdays: 'Domingo_Lunes ...'.split('_'), // 曜日の配列 months: 'Enero_Febrero ... '.split('_'), // 月の配列 - ordinal: n => `${n}º` // 序数 Function (number) => return number + output + ordinal: n => `${n}º`, // 序数 Function (number) => return number + output + relativeTime = { // relative time format strings, keep %s %d as the same + future: 'in %s', // e.g. in 2 hours, %s been replaced with 2hours + past: '%s ago', + s: 'a few seconds', + m: 'a minute', + mm: '%d minutes', + h: 'an hour', + hh: '%d hours', // e.g. 2 hours, %d been replaced with 2 + d: 'a day', + dd: '%d days', + M: 'a month', + MM: '%d months', + y: 'a year', + yy: '%d years' + } } ``` diff --git a/docs/ja/Plugin.md b/docs/ja/Plugin.md index 0c28fa194..1c3836f1e 100644 --- a/docs/ja/Plugin.md +++ b/docs/ja/Plugin.md @@ -26,9 +26,9 @@ dayjs.extend(plugin, options) // プラグインのオプションを指定 ```javascript import dayjs from 'dayjs' -import AdvancedFormat from 'dayjs/plugin/AdvancedFormat' // 必要に応じて読み込み +import advancedFormat from 'dayjs/plugin/advancedFormat' // 必要に応じて読み込み -dayjs.extend(AdvancedFormat) // プラグインを使用 +dayjs.extend(advancedFormat) // プラグインを使用 ``` * CDN を使う場合: @@ -66,6 +66,55 @@ dayjs().format('Q Do k kk X x') | `X` | 1360013296 | Unix タイムスタンプ (秒) | | `x` | 1360013296123 | Unix タイムスタンプ (ミリ秒) | +### RelativeTime + - RelativeTime adds `.from` `.to` `.fromNow` `.toNow` APIs to formats date to relative time strings (e.g. 3 hours ago). + +```javascript +import relativeTime from 'dayjs/plugin/relativeTime' + +dayjs.extend(relativeTime) + +dayjs().from(dayjs('1990')) // 2 years ago +dayjs().from(dayjs(), true) // 2 years + +dayjs().fromNow() + +dayjs().to(dayjs()) + +dayjs().toNow() +``` + +#### Time from now `.fromNow(withoutSuffix?: boolean)` + +Returns the `string` of relative time from now. + +#### Time from X `.from(compared: Dayjs, withoutSuffix?: boolean)` + +Returns the `string` of relative time from X. + +#### Time to now `.toNow(withoutSuffix?: boolean)` + +Returns the `string` of relative time to now. + +#### Time to X `.to(compared: Dayjs, withoutSuffix?: boolean)` + +Returns the `string` of relative time to X. + +| Range | Key | Sample Output | +| ------------------------ | ---- | -------------------------------- | +| 0 to 44 seconds | s | a few seconds ago | +| 45 to 89 seconds | m | a minute ago | +| 90 seconds to 44 minutes | mm | 2 minutes ago ... 44 minutes ago | +| 45 to 89 minutes | h | an hour ago | +| 90 minutes to 21 hours | hh | 2 hours ago ... 21 hours ago | +| 22 to 35 hours | d | a day ago | +| 36 hours to 25 days | dd | 2 days ago ... 25 days ago | +| 26 to 45 days | M | a month ago | +| 46 days to 10 months | MM | 2 months ago ... 10 months ago | +| 11 months to 17months | y | a year ago | +| 18 months+ | yy | 2 years ago ... 20 years ago | + + ## カスタマイズ さまざまなニーズに合わせて独自の Day.js プラグインを構築することができます。 diff --git a/docs/ja/README-ja.md b/docs/ja/README-ja.md index 23b71fce2..aaf1fb895 100644 --- a/docs/ja/README-ja.md +++ b/docs/ja/README-ja.md @@ -86,9 +86,9 @@ dayjs('2018-05-05').locale('zh-cn').format() // 簡体字中国語を特定の プラグインとは、 Day.js の機能を拡張したり、新たな機能を追加するための独立したモジュールのことです。 ```javascript -import AdvancedFormat from 'dayjs/plugin/AdvancedFormat' // 必要に応じて読み込み +import advancedFormat from 'dayjs/plugin/advancedFormat' // 必要に応じて読み込み -dayjs.extend(AdvancedFormat) // プラグインを使用 +dayjs.extend(advancedFormat) // プラグインを使用 dayjs().format('Q Do k kk X x') // 多様なフォーマットが利用可能に ``` diff --git a/docs/pt-br/API-reference.md b/docs/pt-br/API-reference.md index ca3f125e3..ec54d62b2 100644 --- a/docs/pt-br/API-reference.md +++ b/docs/pt-br/API-reference.md @@ -43,6 +43,8 @@ Este objeto `Dayjs` é imutável, ou seja, todas as operações desta API irão * [Igual](#igual) * [Depois](#depois) * [Ano Bissexto](#ano-bissexto) +* [Plugin APIs](#plugin-apis) + * [RelativeTime](#relativetime) --- O Day.js sempre irá retornar um novo objeto `Dayjs` se nada for especificado. @@ -284,6 +286,8 @@ Lista de todos os formatos disponíveis: | `A` | AM PM | | | `a` | am pm | | +* More available formats `Q Do k kk X x ...` in plugin [`AdvancedFormat`](./Plugin.md#advancedformat) + #### Diferença * retorna um Number @@ -430,3 +434,11 @@ Se um ano é bissexto. dayjs().isLeapYear(); dayjs('2000-01-01').isLeapYear(); // true ``` + +## Plugin APIs + +### RelativeTime + +`.from` `.to` `.fromNow` `.toNow` to get relative time + +plugin [`RelativeTime`](./Plugin.md#relativetime) diff --git a/docs/pt-br/I18n.md b/docs/pt-br/I18n.md index edef5e479..6846061e4 100644 --- a/docs/pt-br/I18n.md +++ b/docs/pt-br/I18n.md @@ -38,15 +38,6 @@ dayjs().locale('es').format() // usar locale localmente dayjs('2018-4-28', { locale: es }) // também pode ser feito no constructor ``` -#### Mudando locale apenas para uma chamada da API - -Pesquise os documentos da API para mais detalhes. - -```js -import 'dayjs/locale/es' -dayjs('2018-4-28').format(format, es) // usar o locale es apenas nessa chamada -``` - ## Instalação * Via NPM: @@ -83,7 +74,22 @@ const objetoLocale = { name: 'es', // name: String weekdays: 'Domingo_Lunes ...'.split('_'), // weekdays: Array months: 'Enero_Febrero ... '.split('_'), // months: Array - ordinal: n => `${n}º` // ordinal: Function (number) => return number + saída + ordinal: n => `${n}º`, // ordinal: Function (number) => return number + saída + relativeTime = { // relative time format strings, keep %s %d as the same + future: 'in %s', // e.g. in 2 hours, %s been replaced with 2hours + past: '%s ago', + s: 'a few seconds', + m: 'a minute', + mm: '%d minutes', + h: 'an hour', + hh: '%d hours', // e.g. 2 hours, %d been replaced with 2 + d: 'a day', + dd: '%d days', + M: 'a month', + MM: '%d months', + y: 'a year', + yy: '%d years' + } } ``` diff --git a/docs/pt-br/Plugin.md b/docs/pt-br/Plugin.md index 3a140eed7..f67d2c67e 100644 --- a/docs/pt-br/Plugin.md +++ b/docs/pt-br/Plugin.md @@ -26,9 +26,9 @@ dayjs.extend(plugin, options) // com opções do plugin ```javascript import dayjs from 'dayjs' -import AdvancedFormat from 'dayjs/plugin/AdvancedFormat' // carregar sob demanda +import advancedFormat from 'dayjs/plugin/advancedFormat' // carregar sob demanda -dayjs.extend(AdvancedFormat) // usar plugin +dayjs.extend(advancedFormat) // usar plugin ``` * Via CDN: @@ -65,6 +65,55 @@ Lista de formatos adicionados: | `X` | 1360013296 | Unix Timestamp em segundos | | `x` | 1360013296123 | Unix Timestamp em milissegundos | +### RelativeTime + - RelativeTime adds `.from` `.to` `.fromNow` `.toNow` APIs to formats date to relative time strings (e.g. 3 hours ago). + +```javascript +import relativeTime from 'dayjs/plugin/relativeTime' + +dayjs.extend(relativeTime) + +dayjs().from(dayjs('1990')) // 2 years ago +dayjs().from(dayjs(), true) // 2 years + +dayjs().fromNow() + +dayjs().to(dayjs()) + +dayjs().toNow() +``` + +#### Time from now `.fromNow(withoutSuffix?: boolean)` + +Returns the `string` of relative time from now. + +#### Time from X `.from(compared: Dayjs, withoutSuffix?: boolean)` + +Returns the `string` of relative time from X. + +#### Time to now `.toNow(withoutSuffix?: boolean)` + +Returns the `string` of relative time to now. + +#### Time to X `.to(compared: Dayjs, withoutSuffix?: boolean)` + +Returns the `string` of relative time to X. + +| Range | Key | Sample Output | +| ------------------------ | ---- | -------------------------------- | +| 0 to 44 seconds | s | a few seconds ago | +| 45 to 89 seconds | m | a minute ago | +| 90 seconds to 44 minutes | mm | 2 minutes ago ... 44 minutes ago | +| 45 to 89 minutes | h | an hour ago | +| 90 minutes to 21 hours | hh | 2 hours ago ... 21 hours ago | +| 22 to 35 hours | d | a day ago | +| 36 hours to 25 days | dd | 2 days ago ... 25 days ago | +| 26 to 45 days | M | a month ago | +| 46 days to 10 months | MM | 2 months ago ... 10 months ago | +| 11 months to 17months | y | a year ago | +| 18 months+ | yy | 2 years ago ... 20 years ago | + + ## 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. diff --git a/docs/pt-br/README-pt-br.md b/docs/pt-br/README-pt-br.md index 5cf73613e..1d01f2db4 100644 --- a/docs/pt-br/README-pt-br.md +++ b/docs/pt-br/README-pt-br.md @@ -84,9 +84,9 @@ dayjs('2018-05-05').locale('pt-br').format() // usar locale em português brasil Um plugin é um módulo independente que pode ser adicionado ao Day.js para estendê-lo com mais funcionalidades. ```javascript -import AdvancedFormat from 'dayjs/plugin/AdvancedFormat' // carregar sob demanda +import advancedFormat from 'dayjs/plugin/advancedFormat' // carregar sob demanda -dayjs.extend(AdvancedFormat) // usar plugin +dayjs.extend(advancedFormat) // usar plugin dayjs().format('Q Do k kk X x') // mais formatos disponíveis pelo plugin ``` diff --git a/docs/zh-cn/API-reference.md b/docs/zh-cn/API-reference.md index f21edf1ee..525194024 100644 --- a/docs/zh-cn/API-reference.md +++ b/docs/zh-cn/API-reference.md @@ -43,6 +43,8 @@ * [是否相同](#是否相同) * [是否之后](#是否之后) * [是否闰年](#是否闰年) +* [插件 APIs](#plugin-apis) + * [相对时间](#relativetime) --- 如果没有特别说明,Day.js 的返回值都是新的 `Dayjs` 对象。 @@ -199,26 +201,31 @@ dayjs().format("{YYYY} MM-DDTHH:mm:ssZ[Z]"); // "{2014} 09-08T08:02:17-05:00Z" 详情如下: -| Format | Output | Description | -| ------ | ------ | ----------- | -| `YY` | 18 | 两位数的年份 | -| `YYYY` | 2018 | 四位数的年份 | -| `M` | 1-12 | 月份,从1开始 | -| `MM` | 01-12 | 月份,数字前面加上0 -| `MMM` | Jan-Dec | 简写的月份名称 | -| `MMMM` | January-December | 完整的月份名称 | -| `D` | 1-31 | 月份里的一天 | -| `DD` | 01-31 | 月份里的一天,数字前面加上0 | -| `d` | 0-6 | 一周中的一天,星期天是0 | -| `dddd` | Sunday-Saturday | 一周中一天的名称 | -| `H` | 0-23 | 小时 | -| `HH` | 00-23 | 小时,数字前面加上0 | -| `m` | 0-59 | 分钟 | -| `mm` | 00-59 | 分钟,数字前面加上0 | -| `s` | 0-59 | 秒 | -| `ss` | 00-59 | 秒,数字前面加上0 | -| `Z` | +5:00 | UTC的偏移量 | -| `ZZ` | +0500 | UTC的偏移量,数字前面加上0 | +| Format | Output | Description | +| ------ | ---------------- | ---------------------------- | +| `YY` | 18 | 两位数的年份 | +| `YYYY` | 2018 | 四位数的年份 | +| `M` | 1-12 | 月份,从 1 开始 | +| `MM` | 01-12 | 月份,两位数 | +| `MMM` | Jan-Dec | 简写的月份名称 | +| `MMMM` | January-December | 完整的月份名称 | +| `D` | 1-31 | 月份里的一天 | +| `DD` | 01-31 | 月份里的一天,两位数 | +| `d` | 0-6 | 一周中的一天,星期天是 0 | +| `dddd` | Sunday-Saturday | 一周中一天的名称 | +| `H` | 0-23 | 小时 | +| `HH` | 00-23 | 小时,两位数 | +| `m` | 0-59 | 分钟 | +| `mm` | 00-59 | 分钟,两位数 | +| `s` | 0-59 | 秒 | +| `ss` | 00-59 | 秒 两位数 | +| `SSS` | 000-999 | 秒 三位数 | +| `Z` | +5:00 | UTC 的偏移量 | +| `ZZ` | +0500 | UTC 的偏移量,数字前面加上 0 | +| `A` | AM PM | | +| `a` | am pm | | + +* 更多格式化的选项 `Q Do k kk X x ...` 可以使用插件 [`AdvancedFormat`](./Plugin.md#advancedformat) #### 时间差 - return Number @@ -323,4 +330,12 @@ dayjs().isAfter(dayjs()); // false ```js dayjs().isLeapYear(); dayjs('2000-01-01').isLeapYear(); // true -``` \ No newline at end of file +``` + +## 插件 APIs + +### 相对时间 + +`.from` `.to` `.fromNow` `.toNow` 获得相对时间 + +插件 [`RelativeTime`](./Plugin.md#relativetime) \ No newline at end of file diff --git a/docs/zh-cn/I18n.md b/docs/zh-cn/I18n.md index a13b87272..ce0aada2c 100644 --- a/docs/zh-cn/I18n.md +++ b/docs/zh-cn/I18n.md @@ -41,15 +41,6 @@ dayjs().locale('es').format() // 局部修改语言配置 dayjs('2018-4-28', { locale: es }) // 在新建实例时指定 ``` -#### 修改API调用的语言配置 - -查看相关API文档获取更多细节 - -```js -import 'dayjs/locale/es' -dayjs('2018-4-28').format(format, es) // 只在本次API调用时修改语言配置 -``` - ## 安装 * 通过 NPM: @@ -88,7 +79,22 @@ const localeObject = { name: 'es', // 语言名 String weekdays: 'Domingo_Lunes ...'.split('_'), // 星期 Array months: 'Enero_Febrero ... '.split('_'), // 月份 Array - ordinal: n => `${n}º` // 序号生成工厂 Function (number) => return number + output + ordinal: n => `${n}º`, // 序号生成工厂函数 Function (number) => return number + output + relativeTime = { // 相对时间, %s %d 不用翻译 + future: 'in %s', // e.g. in 2 hours, %s been replaced with 2hours + past: '%s ago', + s: 'a few seconds', + m: 'a minute', + mm: '%d minutes', + h: 'an hour', + hh: '%d hours', // e.g. 2 hours, %d been replaced with 2 + d: 'a day', + dd: '%d days', + M: 'a month', + MM: '%d months', + y: 'a year', + yy: '%d years' + } } ``` diff --git a/docs/zh-cn/Plugin.md b/docs/zh-cn/Plugin.md index bbb351e09..810d2f2c9 100644 --- a/docs/zh-cn/Plugin.md +++ b/docs/zh-cn/Plugin.md @@ -26,9 +26,9 @@ dayjs.extend(plugin, options) // 带参数加载插件 ```javascript import dayjs from 'dayjs' -import AdvancedFormat from 'dayjs/plugin/AdvancedFormat' // 按需加载 +import advancedFormat from 'dayjs/plugin/advancedFormat' // 按需加载 -dayjs.extend(AdvancedFormat) // 使用插件 +dayjs.extend(advancedFormat) // 使用插件 ``` * 通过 CDN: @@ -65,6 +65,54 @@ dayjs().format('Q Do k kk X x') | `X` | 1360013296 | 秒为单位的Unix时间戳 | | `x` | 1360013296123 | 毫秒单位的Unix时间戳 | +### RelativeTime + - RelativeTime 增加了 `.from` `.to` `.fromNow` `.toNow` 4个 API 来展示相对的时间 (e.g. 3 小时以前). + +```javascript +import relativeTime from 'dayjs/plugin/relativeTime' + +dayjs.extend(relativeTime) + +dayjs().from(dayjs('1990')) // 2 年以前 +dayjs().from(dayjs(), true) // 2 年 + +dayjs().fromNow() + +dayjs().to(dayjs()) + +dayjs().toNow() +``` + +#### 距离现在的相对时间 `.fromNow(withoutSuffix?: boolean)` + +返回 `string` 距离现在的相对时间 + +#### 距离 X 的相对时间 `.from(compared: Dayjs, withoutSuffix?: boolean)` + +返回 `string` 距离 X 的相对时间 + +#### 到现在的相对时间 `.toNow(withoutSuffix?: boolean)` + +返回 `string` 到现在的相对时间 + +#### 到 X 的相对时间 `.to(compared: Dayjs, withoutSuffix?: boolean)` + +返回 `string` 到 X 的相对时间 + +| Range | Key | Sample Output | +| ------------------------ | ---- | -------------------------------- | +| 0 到 44 秒 | s | 几秒前 | +| 45 到 89 秒 | m | 1 分钟前 | +| 90 秒 到 44 分 | mm | 2 分钟前 ... 44 分钟前 | +| 45 到 89 分 | h | 1 小时前 | +| 90 分 到 21 小时 | hh | 2 小时前 ... 21 小时前 | +| 22 到 35 小时 | d | 1 天前 | +| 36 小时 到 25 天 | dd | 2 天前 ... 25 天前 | +| 26 到 45 天 | M | 1 个月前 | +| 46 天 到 10 月 | MM | 2 个月前 ... 10 个月前 | +| 11 月 到 17月 | y | 1 年前 | +| 18 月以上 | yy | 2 年前 ... 20 年前 | + ## 自定义 你可以根据需要自由的编写一个Day.js插件 diff --git a/docs/zh-cn/README.zh-CN.md b/docs/zh-cn/README.zh-CN.md index 224a84772..b110642dd 100644 --- a/docs/zh-cn/README.zh-CN.md +++ b/docs/zh-cn/README.zh-CN.md @@ -83,9 +83,9 @@ dayjs('2018-05-05').locale('zh-cn').format() // 在这个实例上使用简体 插件是一些独立的程序,可以给 Day.js 增加新功能和扩展已有功能 ```javascript -import AdvancedFormat from 'dayjs/plugin/AdvancedFormat' // 按需加载插件 +import advancedFormat from 'dayjs/plugin/advancedFormat' // 按需加载插件 -dayjs.extend(AdvancedFormat) // 使用插件 +dayjs.extend(advancedFormat) // 使用插件 dayjs().format('Q Do k kk X x') // 使用扩展后的API ``` diff --git a/src/locale/es.js b/src/locale/es.js index 2598369e9..58312ea33 100644 --- a/src/locale/es.js +++ b/src/locale/es.js @@ -7,7 +7,6 @@ const locale = { relativeTime: { future: 'en %s', past: 'hace %s', - present: 'justo ahora', s: 'unos segundos', m: 'un minuto', mm: '%d minutos', diff --git a/src/locale/id.js b/src/locale/id.js index 2b0e0b39d..da05f219e 100644 --- a/src/locale/id.js +++ b/src/locale/id.js @@ -7,7 +7,6 @@ const locale = { relativeTime: { future: 'dalam %s', past: 'yang lalu %s', - present: 'sekarang', s: 'beberapa detik', m: 'satu menit', mm: '%d menit', diff --git a/src/locale/zh-cn.js b/src/locale/zh-cn.js index 6480ebd23..29c79e11c 100644 --- a/src/locale/zh-cn.js +++ b/src/locale/zh-cn.js @@ -4,7 +4,22 @@ const locale = { name: 'zh-cn', weekdays: '星期日_星期一_星期二_星期三_星期四_星期五_星期六'.split('_'), months: '一月_二月_三月_四月_五月_六月_七月_八月_九月_十月_十一月_十二月'.split('_'), - ordinal: n => n + ordinal: n => n, + relativeTime: { + future: '%s内', + past: '%s前', + s: '几秒', + m: '1 分钟', + mm: '%d 分钟', + h: '1 小时', + hh: '%d 小时', + d: '1 天', + dd: '%d 天', + M: '1 个月', + MM: '%d 个月', + y: '1 年', + yy: '%d 年' + } } dayjs.locale(locale, null, true) diff --git a/src/plugin/advancedFormat.js b/src/plugin/advancedFormat/index.js similarity index 95% rename from src/plugin/advancedFormat.js rename to src/plugin/advancedFormat/index.js index 0820ee25a..1af94b053 100644 --- a/src/plugin/advancedFormat.js +++ b/src/plugin/advancedFormat/index.js @@ -1,4 +1,4 @@ -import { FORMAT_DEFAULT } from '../constant' +import { FORMAT_DEFAULT } from '../../constant' export default (o, c, d) => { // locale needed later const proto = c.prototype diff --git a/src/plugin/fromNow.js b/src/plugin/relativeTime/index.js similarity index 97% rename from src/plugin/fromNow.js rename to src/plugin/relativeTime/index.js index a909cd258..1ac51e431 100644 --- a/src/plugin/fromNow.js +++ b/src/plugin/relativeTime/index.js @@ -1,4 +1,4 @@ -import * as C from '../constant' +import * as C from '../../constant' export default (o, c, d) => { const proto = c.prototype diff --git a/test/locale/keys.test.js b/test/locale/keys.test.js index fc1aa9c04..ac890d762 100644 --- a/test/locale/keys.test.js +++ b/test/locale/keys.test.js @@ -25,7 +25,7 @@ it('Locale keys', () => { expect(dayjs().locale(name).$locale().name).toBe(name) if (relativeTime) { expect(Object.keys(relativeTime).sort()).toEqual(['d', 'dd', 'future', 'h', 'hh', 'm', 'mm', 'M', 'MM', - 'past', 'present', 's', 'y', 'yy'] + 'past', 's', 'y', 'yy'] .sort()) } }) diff --git a/test/plugin/fromNow.test.js b/test/plugin/relativeTime.js similarity index 95% rename from test/plugin/fromNow.test.js rename to test/plugin/relativeTime.js index 2e8ca364a..2bc4a896e 100644 --- a/test/plugin/fromNow.test.js +++ b/test/plugin/relativeTime.js @@ -1,9 +1,9 @@ import MockDate from 'mockdate' import moment from 'moment' import dayjs from '../../src' -import fromNow from '../../src/plugin/fromNow' +import relativeTime from '../../src/plugin/relativeTime' -dayjs.extend(fromNow) +dayjs.extend(relativeTime) beforeEach(() => { MockDate.set(new Date('2018-04-04T16:00:00.000Z'))