Skip to content

Commit 5ca48f0

Browse files
authored
fix(): format API update and locale file update
Merge pull request #228 from iamkun/feature/iamkun update format API
2 parents c223371 + d37f456 commit 5ca48f0

24 files changed

+180
-112
lines changed

docs/en/API-reference.md

+10
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,8 @@ dayjs('2019-01-25').format('DD/MM/YYYY'); // '25/01/2019'
241241
| `D` | 1-31 | The day of the month |
242242
| `DD` | 01-31 | The day of the month, 2-digits |
243243
| `d` | 0-6 | The day of the week, with Sunday as 0 |
244+
| `dd` | Su-Sa | The min name of the day of the week |
245+
| `ddd` | Sun-Sat | The short name of the day of the week |
244246
| `dddd` | Sunday-Saturday | The name of the day of the week |
245247
| `H` | 0-23 | The hour |
246248
| `HH` | 00-23 | The hour, 2-digits |
@@ -378,6 +380,8 @@ dayjs().isAfter(dayjs()); // false
378380

379381
### Is Leap Year `.isLeapYear()`
380382

383+
**[DEPRECATED] in 1.7.0, use [`IsLeapYear plugin`](./Plugin.md#isleapyear) instead**
384+
381385
Returns a `boolean` indicating whether the `Dayjs`'s year is a leap year or not.
382386

383387
```js
@@ -391,3 +395,9 @@ dayjs('2000-01-01').isLeapYear(); // true
391395
`.from` `.to` `.fromNow` `.toNow` to get relative time
392396

393397
plugin [`RelativeTime`](./Plugin.md#relativetime)
398+
399+
### IsLeapYear
400+
401+
`.isLeapYear` to get is a leap year or not
402+
403+
plugin [`IsLeapYear`](./Plugin.md#isleapyear)

docs/en/I18n.md

+3
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,10 @@ Template of a Day.js locale Object.
7878
const localeObject = {
7979
name: 'es', // name String
8080
weekdays: 'Domingo_Lunes ...'.split('_'), // weekdays Array
81+
weekdaysShort: 'Sun_M'.split('_'), // OPTIONAL, short weekdays Array, use first three letters if not provided
82+
weekdaysMin: 'Su_Mo'.split('_'), // OPTIONAL, min weekdays Array, use first two letters if not provided
8183
months: 'Enero_Febrero ... '.split('_'), // months Array
84+
monthsShort: 'Jan_F'.split('_'), // OPTIONAL, short months Array, use first three letters if not provided
8285
ordinal: n => `${n}º`, // ordinal Function (number) => return number + output
8386
relativeTime: { // relative time format strings, keep %s %d as the same
8487
future: 'in %s', // e.g. in 2 hours, %s been replaced with 2hours

docs/en/Plugin.md

+10
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,16 @@ Returns the `string` of relative time to X.
113113
| 11 months to 17months | y | a year ago |
114114
| 18 months+ | yy | 2 years ago ... 20 years ago |
115115

116+
### IsLeapYear
117+
- IsLeapYear adds `.isLeapYear` API to returns a `boolean` indicating whether the `Dayjs`'s year is a leap year or not.
118+
119+
```javascript
120+
import isLeapYear from 'dayjs/plugin/isLeapYear'
121+
122+
dayjs.extend(isLeapYear)
123+
124+
dayjs('2000-01-01').isLeapYear(); // true
125+
```
116126

117127
## Customize
118128

docs/ja/API-reference.md

+10
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,8 @@ dayjs().format('{YYYY} MM-DDTHH:mm:ssZ[Z]'); // "{2014} 09-08T08:02:17-05:00Z"
290290
| `D` | 1-31 ||
291291
| `DD` | 01-31 | 2桁の日 |
292292
| `d` | 0-6 | 曜日 (日曜は0) |
293+
| `dd` | Su-Sa | The min name of the day of the week |
294+
| `ddd` | Sun-Sat | 曜日の略称 |
293295
| `dddd` | Sunday-Saturday | 曜日名 |
294296
| `H` | 0-23 | 時間 |
295297
| `HH` | 00-23 | 2桁の時間 |
@@ -448,6 +450,8 @@ dayjs().isAfter(dayjs()); // false
448450

449451
#### Is Leap Year
450452

453+
**[DEPRECATED] in 1.7.0, use [`IsLeapYear plugin`](./Plugin.md#isleapyear) instead**
454+
451455
* Boolean を返します
452456

453457
その年がうるう年かどうかをチェックします。
@@ -464,3 +468,9 @@ dayjs('2000-01-01').isLeapYear(); // true
464468
`.from` `.to` `.fromNow` `.toNow` to get relative time
465469

466470
plugin [`RelativeTime`](./Plugin.md#relativetime)
471+
472+
### IsLeapYear
473+
474+
`.isLeapYear` to get is a leap year or not
475+
476+
plugin [`IsLeapYear`](./Plugin.md#isleapyear)

docs/ja/I18n.md

+3
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,10 @@ dayjs().locale('es').format() // ロケールを特定のインスタンスに
8080
const localeObject = {
8181
name: 'es', // ロケール名を表す文字列
8282
weekdays: 'Domingo_Lunes ...'.split('_'), // 曜日の配列
83+
weekdaysShort: 'Sun_M'.split('_'), // OPTIONAL, short weekdays Array, use first three letters if not provided
84+
weekdaysMin: 'Su_Mo'.split('_'), // OPTIONAL, min weekdays Array, use first two letters if not provided
8385
months: 'Enero_Febrero ... '.split('_'), // 月の配列
86+
monthsShort: 'Jan_F'.split('_'), // OPTIONAL, short months Array, use first three letters if not provided
8487
ordinal: n => `${n}º`, // 序数 Function (number) => return number + output
8588
relativeTime: { // relative time format strings, keep %s %d as the same
8689
future: 'in %s', // e.g. in 2 hours, %s been replaced with 2hours

docs/ja/Plugin.md

+10
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,16 @@ Returns the `string` of relative time to X.
114114
| 11 months to 17months | y | a year ago |
115115
| 18 months+ | yy | 2 years ago ... 20 years ago |
116116

117+
### IsLeapYear
118+
- IsLeapYear adds `.isLeapYear` API to returns a `boolean` indicating whether the `Dayjs`'s year is a leap year or not.
119+
120+
```javascript
121+
import isLeapYear from 'dayjs/plugin/isLeapYear'
122+
123+
dayjs.extend(isLeapYear)
124+
125+
dayjs('2000-01-01').isLeapYear(); // true
126+
```
117127

118128
## カスタマイズ
119129

docs/ko/API-reference.md

+10
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,8 @@ dayjs('2019-01-25').format('DD/MM/YYYY'); // '25/01/2019'
241241
| `D` | 1-31 ||
242242
| `DD` | 01-31 | 일, 두 자리로 표현 |
243243
| `d` | 0-6 | 요일, 일요일은 0 |
244+
| `dd` | Su-Sa | The min name of the day of the week |
245+
| `ddd` | Sun-Sat | The short name of the day of the week |
244246
| `dddd` | Sunday-Saturday | 요일 이름 |
245247
| `H` | 0-23 | 시간 |
246248
| `HH` | 00-23 | 시간, 두 자리로 표현 |
@@ -378,6 +380,8 @@ dayjs().isAfter(dayjs()); // false
378380

379381
### Is Leap Year `.isLeapYear()`
380382

383+
**[DEPRECATED] in 1.7.0, use [`IsLeapYear plugin`](./Plugin.md#isleapyear) instead**
384+
381385
`Dayjs` 값이 윤년인지를 확인합니다. 반환 타입은 `boolean` 입니다.
382386

383387
```js
@@ -391,3 +395,9 @@ dayjs('2000-01-01').isLeapYear(); // true
391395
`.from` `.to` `.fromNow` `.toNow`에 대한 상대 시간을 가져옵니다.
392396

393397
플러그인 [`RelativeTime`](./Plugin.md#relativetime)
398+
399+
### IsLeapYear
400+
401+
`.isLeapYear` to get is a leap year or not
402+
403+
plugin [`IsLeapYear`](./Plugin.md#isleapyear)

docs/ko/I18n.md

+3
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,10 @@ Day.js locale 오브젝트 템플릿 입니다.
7878
const localeObject = {
7979
name: 'es', // name String
8080
weekdays: 'Domingo_Lunes ...'.split('_'), // weekdays Array
81+
weekdaysShort: 'Sun_M'.split('_'), // OPTIONAL, short weekdays Array, use first three letters if not provided
82+
weekdaysMin: 'Su_Mo'.split('_'), // OPTIONAL, min weekdays Array, use first two letters if not provided
8183
months: 'Enero_Febrero ... '.split('_'), // months Array
84+
monthsShort: 'Jan_F'.split('_'), // OPTIONAL, short months Array, use first three letters if not provided
8285
ordinal: n => `${n}º`, // ordinal Function (number) => return number + output
8386
relativeTime = { // relative time format strings, keep %s %d as the same
8487
future: 'in %s', // e.g. in 2 hours, %s been replaced with 2hours

docs/ko/Plugin.md

+10
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,16 @@ X 시간부터 상대시간을 `string`으로 반환합니다.
113113
| 11 달 ~ 17 달 | y | 일년 전 |
114114
| 18 달 이상 | yy | 2 년 전 ~ 20 년 전 |
115115

116+
### IsLeapYear
117+
- IsLeapYear adds `.isLeapYear` API to returns a `boolean` indicating whether the `Dayjs`'s year is a leap year or not.
118+
119+
```javascript
120+
import isLeapYear from 'dayjs/plugin/isLeapYear'
121+
122+
dayjs.extend(isLeapYear)
123+
124+
dayjs('2000-01-01').isLeapYear(); // true
125+
```
116126

117127
## Customize
118128

docs/pt-br/API-reference.md

+10
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,8 @@ Lista de todos os formatos disponíveis:
271271
| `D` | 1-31 | Dia do mês |
272272
| `DD` | 01-31 | Dia do mês, com 2 dígitos |
273273
| `d` | 0-6 | Dia da semana (Domingo = 0) |
274+
| `dd` | Su-Sa | The min name of the day of the week |
275+
| `ddd` | Sun-Sat | The short name of the day of the week |
274276
| `dddd` | Sunday-Saturday | Nome do dia da semana |
275277
| `H` | 0-23 | Hora |
276278
| `HH` | 00-23 | Hora, com 2 dígitos |
@@ -426,6 +428,8 @@ dayjs().isAfter(dayjs()); // false
426428

427429
#### Ano Bissexto
428430

431+
**[DEPRECATED] in 1.7.0, use [`IsLeapYear plugin`](./Plugin.md#isleapyear) instead**
432+
429433
* retorna um Boolean
430434

431435
Se um ano é bissexto.
@@ -442,3 +446,9 @@ dayjs('2000-01-01').isLeapYear(); // true
442446
`.from` `.to` `.fromNow` `.toNow` to get relative time
443447

444448
plugin [`RelativeTime`](./Plugin.md#relativetime)
449+
450+
### IsLeapYear
451+
452+
`.isLeapYear` to get is a leap year or not
453+
454+
plugin [`IsLeapYear`](./Plugin.md#isleapyear)

docs/pt-br/I18n.md

+3
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,10 @@ Template de um objeto de locale do Day.js.
7373
const objetoLocale = {
7474
name: 'es', // name: String
7575
weekdays: 'Domingo_Lunes ...'.split('_'), // weekdays: Array
76+
weekdaysShort: 'Sun_M'.split('_'), // OPTIONAL, short weekdays Array, use first three letters if not provided
77+
weekdaysMin: 'Su_Mo'.split('_'), // OPTIONAL, min weekdays Array, use first two letters if not provided
7678
months: 'Enero_Febrero ... '.split('_'), // months: Array
79+
monthsShort: 'Jan_F'.split('_'), // OPTIONAL, short months Array, use first three letters if not provided
7780
ordinal: n => `${n}º`, // ordinal: Function (number) => return number + saída
7881
relativeTime: { // relative time format strings, keep %s %d as the same
7982
future: 'in %s', // e.g. in 2 hours, %s been replaced with 2hours

docs/pt-br/Plugin.md

+10
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,16 @@ Returns the `string` of relative time to X.
113113
| 11 months to 17months | y | a year ago |
114114
| 18 months+ | yy | 2 years ago ... 20 years ago |
115115

116+
### IsLeapYear
117+
- IsLeapYear adds `.isLeapYear` API to returns a `boolean` indicating whether the `Dayjs`'s year is a leap year or not.
118+
119+
```javascript
120+
import isLeapYear from 'dayjs/plugin/isLeapYear'
121+
122+
dayjs.extend(isLeapYear)
123+
124+
dayjs('2000-01-01').isLeapYear(); // true
125+
```
116126

117127
## Customizar
118128

docs/zh-cn/API-reference.md

+12-1
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,8 @@ dayjs().format("{YYYY} MM-DDTHH:mm:ssZ[Z]"); // "{2014} 09-08T08:02:17-05:00Z"
212212
| `D` | 1-31 | 月份里的一天 |
213213
| `DD` | 01-31 | 月份里的一天,两位数 |
214214
| `d` | 0-6 | 一周中的一天,星期天是 0 |
215+
| `dd` | Su-Sa | 最简写的一周中一天的名称 |
216+
| `ddd` | Sun-Sat | 简写的一周中一天的名称 |
215217
| `dddd` | Sunday-Saturday | 一周中一天的名称 |
216218
| `H` | 0-23 | 小时 |
217219
| `HH` | 00-23 | 小时,两位数 |
@@ -324,6 +326,9 @@ dayjs().isAfter(Dayjs);
324326
dayjs().isAfter(dayjs()); // false
325327
```
326328
#### 是否闰年
329+
330+
**[已废弃] 将在 1.7.0 废弃, 使用 [`IsLeapYear 插件`](./Plugin.md#isleapyear) 代替**
331+
327332
- return Boolean
328333

329334
是否闰年。
@@ -338,4 +343,10 @@ dayjs('2000-01-01').isLeapYear(); // true
338343

339344
`.from` `.to` `.fromNow` `.toNow` 获得相对时间
340345

341-
插件 [`RelativeTime`](./Plugin.md#relativetime)
346+
插件 [`RelativeTime`](./Plugin.md#relativetime)
347+
348+
### IsLeapYear
349+
350+
`.isLeapYear` 获得是否闰年
351+
352+
插件 [`IsLeapYear`](./Plugin.md#isleapyear)

docs/zh-cn/I18n.md

+3
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,10 @@ Day.js 的语言配置模版
7878
const localeObject = {
7979
name: 'es', // 语言名 String
8080
weekdays: 'Domingo_Lunes ...'.split('_'), // 星期 Array
81+
weekdaysShort: 'Sun_M'.split('_'), // 可选, 短的星期 Array, 如果没提供则使用前三个字符
82+
weekdaysMin: 'Su_Mo'.split('_'), // 可选, 最短的星期 Array, 如果没提供则使用前两个字符
8183
months: 'Enero_Febrero ... '.split('_'), // 月份 Array
84+
monthsShort: 'Jan_F'.split('_'), // 可选, 短的月份 Array, 如果没提供则使用前三个字符
8285
ordinal: n => `${n}º`, // 序号生成工厂函数 Function (number) => return number + output
8386
relativeTime: { // 相对时间, %s %d 不用翻译
8487
future: 'in %s', // e.g. in 2 hours, %s been replaced with 2hours

docs/zh-cn/Plugin.md

+11
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,17 @@ dayjs().toNow()
113113
| 11 月 到 17月 | y | 1 年前 |
114114
| 18 月以上 | yy | 2 年前 ... 20 年前 |
115115

116+
## IsLeapYear
117+
- IsLeapYear 增加了 `.isLeapYear` API 返回一个 `boolean` 来展示一个 `Dayjs`'s 的年份是不是闰年.
118+
119+
```javascript
120+
import isLeapYear from 'dayjs/plugin/isLeapYear'
121+
122+
dayjs.extend(isLeapYear)
123+
124+
dayjs('2000-01-01').isLeapYear(); // true
125+
```
126+
116127
## 自定义
117128

118129
你可以根据需要自由的编写一个Day.js插件

src/index.js

+10-5
Original file line numberDiff line numberDiff line change
@@ -274,13 +274,16 @@ class Dayjs {
274274
}
275275

276276

277-
format(formatStr, localeObject) {
277+
format(formatStr) {
278278
const str = formatStr || C.FORMAT_DEFAULT
279279
const zoneStr = Utils.padZoneStr(this.$d.getTimezoneOffset())
280-
const locale = localeObject || this.$locale()
280+
const locale = this.$locale()
281281
const {
282-
weekdays, months, weekdaysShort, monthsShort
282+
weekdays, months
283283
} = locale
284+
const getShort = (arr, index, full, length) => (
285+
(arr && arr[index]) || full[index].substr(0, length)
286+
)
284287
return str.replace(C.REGEX_FORMAT, (match) => {
285288
if (match.indexOf('[') > -1) return match.replace(/\[|\]/g, '')
286289
switch (match) {
@@ -293,7 +296,7 @@ class Dayjs {
293296
case 'MM':
294297
return Utils.padStart(this.$M + 1, 2, '0')
295298
case 'MMM':
296-
return (monthsShort && monthsShort[this.$M]) || months[this.$M].slice(0, 3)
299+
return getShort(locale.monthsShort, this.$M, months, 3)
297300
case 'MMMM':
298301
return months[this.$M]
299302
case 'D':
@@ -302,8 +305,10 @@ class Dayjs {
302305
return Utils.padStart(this.$D, 2, '0')
303306
case 'd':
304307
return String(this.$W)
308+
case 'dd':
309+
return getShort(locale.weekdaysMin, this.$W, weekdays, 2)
305310
case 'ddd':
306-
return (weekdaysShort && weekdaysShort[this.$W]) || weekdays[this.$M].substr(0, 3)
311+
return getShort(locale.weekdaysShort, this.$W, weekdays, 3)
307312
case 'dddd':
308313
return weekdays[this.$W]
309314
case 'H':

src/locale/th.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import dayjs from 'dayjs'
33
const locale = {
44
name: 'th',
55
weekdays: 'อาทิตย์_จันทร์_อังคาร_พุธ_พฤหัสบดี_ศุกร์_เสาร์'.split('_'),
6-
weekdaysShort: 'อา._จ._อ._พ._พฤ._ศ._ส.'.split('_'),
6+
weekdaysShort: 'อาทิตย์_จันทร์_อังคาร_พุธ_พฤหัส_ศุกร์_เสาร์'.split('_'),
7+
weekdaysMin: 'อา._จ._อ._พ._พฤ._ศ._ส.'.split('_'),
78
months: 'มกราคม_กุมภาพันธ์_มีนาคม_เมษายน_พฤษภาคม_มิถุนายน_กรกฏาคม_สิงหาคม_กันยายน_ตุลาคม_พฤศจิกายน_ธันวาคม'.split('_'),
89
monthsShort: 'ม.ค._ก.พ._มี.ค._เม.ย._พ.ค._มิ.ย._ก.ค._ส.ค._ก.ย._ต.ค._พ.ย._ธ.ค.'.split('_'),
910
relativeTime: {

src/locale/zh-cn.js

+3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ import dayjs from 'dayjs'
33
const locale = {
44
name: 'zh-cn',
55
weekdays: '星期日_星期一_星期二_星期三_星期四_星期五_星期六'.split('_'),
6+
weekdaysShort: '周日_周一_周二_周三_周四_周五_周六'.split('_'),
7+
weekdaysMin: '日_一_二_三_四_五_六'.split('_'),
68
months: '一月_二月_三月_四月_五月_六月_七月_八月_九月_十月_十一月_十二月'.split('_'),
9+
monthsShort: '1月_2月_3月_4月_5月_6月_7月_8月_9月_10月_11月_12月'.split('_'),
710
ordinal: n => n,
811
relativeTime: {
912
future: '%s内',

src/plugin/isLeapYear/index.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export default (o, c) => {
2+
const proto = c.prototype
3+
proto.isLeapYear = function () {
4+
return ((this.$y % 4 === 0) && (this.$y % 100 !== 0)) || (this.$y % 400 === 0)
5+
}
6+
}
7+

test/display.test.js

+9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import moment from 'moment'
22
import MockDate from 'mockdate'
33
import dayjs from '../src'
4+
import th from '../src/locale/th'
45

56
beforeEach(() => {
67
MockDate.set(new Date())
@@ -33,6 +34,8 @@ it('Format Day of Month D DD 1 - 31', () => {
3334

3435
it('Format Day of Week d Sun - Sat', () => {
3536
expect(dayjs().format('d')).toBe(moment().format('d'))
37+
expect(dayjs().format('dd')).toBe(moment().format('dd'))
38+
expect(dayjs().format('ddd')).toBe(moment().format('ddd'))
3639
expect(dayjs().format('dddd')).toBe(moment().format('dddd'))
3740
})
3841

@@ -95,6 +98,12 @@ it('Format Time Zone ZZ', () => {
9598
expect(dayjs().format('ZZ')).toBe(moment().format('ZZ'))
9699
})
97100

101+
it('Format ddd dd MMM with short locale', () => {
102+
expect(dayjs().locale(th).format('dd')).toBe(moment().locale('th').format('dd'))
103+
expect(dayjs().locale(th).format('ddd')).toBe(moment().locale('th').format('ddd'))
104+
expect(dayjs().locale(th).format('MMM')).toBe(moment().locale('th').format('MMM'))
105+
})
106+
98107
it('Format Complex with other string - : / ', () => {
99108
const string = 'YY-M-D / HH:mm:ss'
100109
expect(dayjs().format(string)).toBe(moment().format(string))

test/locale.test.js

-5
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,6 @@ it('set locale for one instance only', () => {
3232
.toBe('Saturday 28, April')
3333
})
3434

35-
it('set locale for this line only', () => {
36-
expect(dayjs('2018-4-28').format(format, es))
37-
.toBe('Sábado 28, Abril')
38-
})
39-
4035
it('set global locale', () => {
4136
dayjs.locale('en')
4237
expect(dayjs('2018-4-28').format(format))

0 commit comments

Comments
 (0)