Skip to content

Commit

Permalink
fix(): add RelativeTime plugin and locale file update
Browse files Browse the repository at this point in the history
Chore update RelativeTime with DOCs Merge pull request #198 from iamkun/feature/iamkun
  • Loading branch information
iamkun authored May 25, 2018
2 parents d7ed7cb + 930cf3c commit c1fbbca
Show file tree
Hide file tree
Showing 24 changed files with 370 additions and 87 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand Down
4 changes: 2 additions & 2 deletions build/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)}`
}))
})
Expand Down
12 changes: 12 additions & 0 deletions docs/en/API-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)
26 changes: 16 additions & 10 deletions docs/en/I18n.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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'
}
}
```

Expand Down
53 changes: 51 additions & 2 deletions docs/en/Plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -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')
```
Expand All @@ -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.
Expand Down
12 changes: 12 additions & 0 deletions docs/ja/API-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

---

Expand Down Expand Up @@ -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 を返します
Expand Down Expand Up @@ -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)
26 changes: 16 additions & 10 deletions docs/ja/I18n.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 を使う場合:
Expand Down Expand Up @@ -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'
}
}
```
Expand Down
53 changes: 51 additions & 2 deletions docs/ja/Plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 を使う場合:
Expand Down Expand Up @@ -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 プラグインを構築することができます。
Expand Down
4 changes: 2 additions & 2 deletions docs/ja/README-ja.md
Original file line number Diff line number Diff line change
Expand Up @@ -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') // 多様なフォーマットが利用可能に
```
Expand Down
12 changes: 12 additions & 0 deletions docs/pt-br/API-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
26 changes: 16 additions & 10 deletions docs/pt-br/I18n.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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'
}
}
```

Expand Down
Loading

0 comments on commit c1fbbca

Please sign in to comment.