Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Update advancedFormat plugin to support w ww wo week tokens … #678

Merged
merged 3 commits into from
Oct 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions docs/en/Plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,18 @@ dayjs().format('Q Do k kk X x')

List of added formats:

| Format | Output | Description |
| ------ | ---------------- | ---------------------------------- |
| `Q` | 1-4 | Quarter |
| `Do` | 1st 2nd ... 31st | Day of Month with ordinal |
| `k` | 1-24 | The hour, beginning at 1 |
| `kk` | 01-24 | The hour, 2-digits, beginning at 1 |
| `X` | 1360013296 | Unix Timestamp in second |
| `x` | 1360013296123 | Unix Timestamp in millisecond |
| Format | Output | Description |
| ------ | --------------------- | ----------------------------------------------------- |
| `Q` | 1-4 | Quarter |
| `Do` | 1st 2nd ... 31st | Day of Month with ordinal |
| `k` | 1-24 | The hour, beginning at 1 |
| `kk` | 01-24 | The hour, 2-digits, beginning at 1 |
| `X` | 1360013296 | Unix Timestamp in second |
| `x` | 1360013296123 | Unix Timestamp in millisecond |
| `w` | 1 2 ... 52 53 | Week of year (depend: weekOfYear plugin) |
| `ww` | 01 02 ... 52 53 | Week of year, 2-digits (depend: weekOfYear plugin) |
| `wo` | 1st 2nd ... 52nd 53rd | Week of year with ordinal (depend: weekOfYear plugin) |
| `gggg` | 2017 | Week Year (depend: weekYear plugin) |

### LocalizedFormat

Expand Down
20 changes: 12 additions & 8 deletions docs/es-es/Plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,18 @@ dayjs().format('Q Do k kk X x')

Lista de formatos añadidos:

| Formato | Salida | Descripción |
| ------- | ------------- | ------------------------------------- |
| `Q` | 1-4 | Cuarto |
| `Do` | 1º 2º ... 31º | Día del mes con ordinal |
| `k` | 1-24 | Hora, contando desde 1 |
| `kk` | 01-24 | Hora, con 2 dígitos, contando desde 1 |
| `X` | 1360013296 | Tiempo Unix en segundos |
| `x` | 1360013296123 | Tiempo Unix en milisegundos |
| Formato | Salida | Descripción |
| ------- | --------------------- | ----------------------------------------------------- |
| `Q` | 1-4 | Cuarto |
| `Do` | 1º 2º ... 31º | Día del mes con ordinal |
| `k` | 1-24 | Hora, contando desde 1 |
| `kk` | 01-24 | Hora, con 2 dígitos, contando desde 1 |
| `X` | 1360013296 | Tiempo Unix en segundos |
| `x` | 1360013296123 | Tiempo Unix en milisegundos |
| `w` | 1 2 ... 52 53 | Week of year (depend: weekOfYear plugin) |
| `ww` | 01 02 ... 52 53 | Week of year, 2-digits (depend: weekOfYear plugin) |
| `wo` | 1st 2nd ... 52nd 53rd | Week of year with ordinal (depend: weekOfYear plugin) |
| `gggg` | 2017 | Week Year (depend: weekYear plugin) |

### LocalizedFormat

Expand Down
20 changes: 12 additions & 8 deletions docs/ja/Plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,18 @@ dayjs().format('Q Do k kk X x')

追加されるフォーマットの一覧:

| フォーマット | 出力 | 説明 |
| ------------ | ---------------- | ---------------------------- |
| `Q` | 1-4 | 四半期 |
| `Do` | 1st 2nd ... 31st | 序数付きの日 |
| `k` | 1-24 | 1 始まりの時間 |
| `kk` | 01-24 | 1 始まりで 2 桁の時間 |
| `X` | 1360013296 | Unix タイムスタンプ (秒) |
| `x` | 1360013296123 | Unix タイムスタンプ (ミリ秒) |
| フォーマット | 出力 | 説明 |
| ------------ | --------------------- | ----------------------------------------------------- |
| `Q` | 1-4 | 四半期 |
| `Do` | 1st 2nd ... 31st | 序数付きの日 |
| `k` | 1-24 | 1 始まりの時間 |
| `kk` | 01-24 | 1 始まりで 2 桁の時間 |
| `X` | 1360013296 | Unix タイムスタンプ (秒) |
| `x` | 1360013296123 | Unix タイムスタンプ (ミリ秒) |
| `w` | 1 2 ... 52 53 | Week of year (depend: weekOfYear plugin) |
| `ww` | 01 02 ... 52 53 | Week of year, 2-digits (depend: weekOfYear plugin) |
| `wo` | 1st 2nd ... 52nd 53rd | Week of year with ordinal (depend: weekOfYear plugin) |
| `gggg` | 2017 | Week Year (depend: weekYear plugin) |

### LocalizedFormat

Expand Down
20 changes: 12 additions & 8 deletions docs/ko/Plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,18 @@ dayjs().format('Q Do k kk X x')

추가된 형식 목록:

| Format | Output | Description |
| ------ | ---------------- | ---------------------------- |
| `Q` | 1-4 | 분기 |
| `Do` | 1st 2nd ... 31st | 서수형식의 일자 명 |
| `k` | 1-24 | 시간, 1부터 시작 |
| `kk` | 01-24 | 시간, 2자리 표현, 1부터 시작 |
| `X` | 1360013296 | 유닉스 타임스템프, 초 |
| `x` | 1360013296123 | 유닉스 타임스탬프, 밀리 초 |
| Format | Output | Description |
| ------ | --------------------- | ----------------------------------------------------- |
| `Q` | 1-4 | 분기 |
| `Do` | 1st 2nd ... 31st | 서수형식의 일자 명 |
| `k` | 1-24 | 시간, 1부터 시작 |
| `kk` | 01-24 | 시간, 2자리 표현, 1부터 시작 |
| `X` | 1360013296 | 유닉스 타임스템프, 초 |
| `x` | 1360013296123 | 유닉스 타임스탬프, 밀리 초 |
| `w` | 1 2 ... 52 53 | Week of year (depend: weekOfYear plugin) |
| `ww` | 01 02 ... 52 53 | Week of year, 2-digits (depend: weekOfYear plugin) |
| `wo` | 1st 2nd ... 52nd 53rd | Week of year with ordinal (depend: weekOfYear plugin) |
| `gggg` | 2017 | Week Year (depend: weekYear plugin) |

### LocalizedFormat

Expand Down
2 changes: 1 addition & 1 deletion docs/pt-br/I18n.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ dayjs.locale('es') // usar a localidade 'es' globalmente
dayjs.locale('de-german', de) // usar a localidade 'de' e alterar a string padrão
const customizedLocaleObject = { ... } // mais detalhes podem ser vistos na sessão de customização abaixo
dayjs.locale(customizedLocaleObject) // usar uma localidade customizada
dayjs.locale('en') // alterna de volta a localidade globalmente para padrão em inglês
dayjs.locale('en') // alterna de volta a localidade globalmente para padrão em inglês
```

- Mudar o _locale_ global não afeta instâncias já existentes.
Expand Down
20 changes: 12 additions & 8 deletions docs/pt-br/Plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,18 @@ dayjs().format('Q Do k kk X x')

Lista de formatos adicionados:

| Formato | Saída | Descrição |
| ------- | ---------------- | ------------------------------------ |
| `Q` | 1-4 | Quarter |
| `Do` | 1st 2nd ... 31st | Dia do mês com ordinal |
| `k` | 1-24 | Hora (começando do 1) |
| `kk` | 01-24 | Hora, com 2 dígitos (começando do 1) |
| `X` | 1360013296 | Unix Timestamp em segundos |
| `x` | 1360013296123 | Unix Timestamp em milissegundos |
| Formato | Saída | Descrição |
| ------- | --------------------- | ----------------------------------------------------- |
| `Q` | 1-4 | Quarter |
| `Do` | 1st 2nd ... 31st | Dia do mês com ordinal |
| `k` | 1-24 | Hora (começando do 1) |
| `kk` | 01-24 | Hora, com 2 dígitos (começando do 1) |
| `X` | 1360013296 | Unix Timestamp em segundos |
| `x` | 1360013296123 | Unix Timestamp em milissegundos |
| `w` | 1 2 ... 52 53 | Week of year (depend: weekOfYear plugin) |
| `ww` | 01 02 ... 52 53 | Week of year, 2-digits (depend: weekOfYear plugin) |
| `wo` | 1st 2nd ... 52nd 53rd | Week of year with ordinal (depend: weekOfYear plugin) |
| `gggg` | 2017 | Week Year (depend: weekYear plugin) |

### LocalizedFormat

Expand Down
2 changes: 1 addition & 1 deletion docs/zh-cn/API-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ dayjs().format('{YYYY} MM-DDTHH:mm:ssZ[Z]') // "{2014} 09-08T08:02:17-05:00Z"
```js
const date1 = dayjs('2019-01-25')
const date2 = dayjs('2018-06-05')
date1.diff(date2) // 20214000000
date1.diff(date2) // 20214000000
date1.diff(date2, 'month') // 7
date1.diff(date2, 'month', true) // 7.645161290322581
date1.diff(date2, 'day') // 233
Expand Down
20 changes: 12 additions & 8 deletions docs/zh-cn/Plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,18 @@ dayjs().format('Q Do k kk X x')

扩展的模版列表:

| 模版 | 输出 | 简介 |
| ---- | ---------------- | ---------------------- |
| `Q` | 1-4 | 季度 |
| `Do` | 1st 2nd ... 31st | 带序号的月份 |
| `k` | 1-24 | 时:由 1 开始 |
| `kk` | 01-24 | 时:由 1 开始,二位数 |
| `X` | 1360013296 | 秒为单位的 Unix 时间戳 |
| `x` | 1360013296123 | 毫秒单位的 Unix 时间戳 |
| 模版 | 输出 | 简介 |
| ------ | --------------------- | ------------------------------------------ |
| `Q` | 1-4 | 季度 |
| `Do` | 1st 2nd ... 31st | 带序号的月份 |
| `k` | 1-24 | 时:由 1 开始 |
| `kk` | 01-24 | 时:由 1 开始,二位数 |
| `X` | 1360013296 | 秒为单位的 Unix 时间戳 |
| `x` | 1360013296123 | 毫秒单位的 Unix 时间戳 |
| `w` | 1 2 ... 52 53 | 年中第几周 (依赖: weekOfYear 插件) |
| `ww` | 01 02 ... 52 53 | 年中第几周,二位数 (依赖: weekOfYear 插件) |
| `wo` | 1st 2nd ... 52nd 53rd | 带序号的年中第几周 (依赖: weekOfYear 插件) |
| `gggg` | 2017 | 根据周计算的年份 (依赖: weekYear 插件) |

### LocalizedFormat

Expand Down
5 changes: 4 additions & 1 deletion src/plugin/advancedFormat/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default (o, c, d) => { // locale needed later
const locale = this.$locale()
const utils = this.$utils()
const str = formatStr || FORMAT_DEFAULT
const result = str.replace(/\[([^\]]+)]|Q|wo|gggg|Do|X|x|k{1,2}|S/g, (match) => {
const result = str.replace(/\[([^\]]+)]|Q|wo|ww|w|gggg|Do|X|x|k{1,2}|S/g, (match) => {
switch (match) {
case 'Q':
return Math.ceil((this.$M + 1) / 3)
Expand All @@ -23,6 +23,9 @@ export default (o, c, d) => { // locale needed later
return this.weekYear()
case 'wo':
return locale.ordinal(this.week(), 'W') // W for week
case 'w':
case 'ww':
return utils.s(this.week(), match === 'w' ? 1 : 2, '0')
case 'k':
case 'kk':
return utils.s(String(this.$H === 0 ? 24 : this.$H), match === 'k' ? 1 : 2, '0')
Expand Down
9 changes: 9 additions & 0 deletions test/plugin/weekOfYear.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import moment from 'moment'
import MockDate from 'mockdate'
import dayjs from '../../src'
import weekOfYear from '../../src/plugin/weekOfYear'
import advancedFormat from '../../src/plugin/advancedFormat'
import '../../src/locale/en-gb'

dayjs.extend(advancedFormat)
dayjs.extend(weekOfYear)

beforeEach(() => {
Expand Down Expand Up @@ -39,3 +41,10 @@ it('Week of year with locale', () => {
expect(dayjs('2018-12-30').week()).toBe(moment('2018-12-30').week())
expect(dayjs('2019-12-29').week()).toBe(moment('2019-12-29').week())
})

it('Format w ww wo', () => {
const day = '2019-07-28'
const D = dayjs(day)
const M = moment(day)
expect(D.format('w ww wo')).toBe(M.format('w ww wo'))
})