From 96f234a0d861d3e4880c7bb2ad30c22dffbeea8e Mon Sep 17 00:00:00 2001 From: iamkun Date: Wed, 6 Jun 2018 14:43:13 +0800 Subject: [PATCH 01/10] test: add short locale test --- src/index.js | 2 +- src/locale/th.js | 2 +- test/display.test.js | 7 +++ test/locale.th.test.js | 99 ------------------------------------------ 4 files changed, 9 insertions(+), 101 deletions(-) delete mode 100644 test/locale.th.test.js diff --git a/src/index.js b/src/index.js index 734420a7b..d396caa23 100644 --- a/src/index.js +++ b/src/index.js @@ -303,7 +303,7 @@ class Dayjs { case 'd': return String(this.$W) case 'ddd': - return (weekdaysShort && weekdaysShort[this.$W]) || weekdays[this.$M].substr(0, 3) + return (weekdaysShort && weekdaysShort[this.$W]) || weekdays[this.$W].substr(0, 3) case 'dddd': return weekdays[this.$W] case 'H': diff --git a/src/locale/th.js b/src/locale/th.js index 05449e411..31d7b09f2 100644 --- a/src/locale/th.js +++ b/src/locale/th.js @@ -3,7 +3,7 @@ import dayjs from 'dayjs' const locale = { name: 'th', weekdays: 'อาทิตย์_จันทร์_อังคาร_พุธ_พฤหัสบดี_ศุกร์_เสาร์'.split('_'), - weekdaysShort: 'อา._จ._อ._พ._พฤ._ศ._ส.'.split('_'), + weekdaysShort: 'อาทิตย์_จันทร์_อังคาร_พุธ_พฤหัส_ศุกร์_เสาร์'.split('_'), months: 'มกราคม_กุมภาพันธ์_มีนาคม_เมษายน_พฤษภาคม_มิถุนายน_กรกฏาคม_สิงหาคม_กันยายน_ตุลาคม_พฤศจิกายน_ธันวาคม'.split('_'), monthsShort: 'ม.ค._ก.พ._มี.ค._เม.ย._พ.ค._มิ.ย._ก.ค._ส.ค._ก.ย._ต.ค._พ.ย._ธ.ค.'.split('_'), relativeTime: { diff --git a/test/display.test.js b/test/display.test.js index 2574af01a..dc5c72d65 100644 --- a/test/display.test.js +++ b/test/display.test.js @@ -1,6 +1,7 @@ import moment from 'moment' import MockDate from 'mockdate' import dayjs from '../src' +import th from '../src/locale/th' beforeEach(() => { MockDate.set(new Date()) @@ -33,6 +34,7 @@ it('Format Day of Month D DD 1 - 31', () => { it('Format Day of Week d Sun - Sat', () => { expect(dayjs().format('d')).toBe(moment().format('d')) + expect(dayjs().format('ddd')).toBe(moment().format('ddd')) expect(dayjs().format('dddd')).toBe(moment().format('dddd')) }) @@ -95,6 +97,11 @@ it('Format Time Zone ZZ', () => { expect(dayjs().format('ZZ')).toBe(moment().format('ZZ')) }) +it('Format ddd MMM with short locale', () => { + expect(dayjs().locale(th).format('ddd')).toBe(moment().locale('th').format('ddd')) + expect(dayjs().locale(th).format('MMM')).toBe(moment().locale('th').format('MMM')) +}) + it('Format Complex with other string - : / ', () => { const string = 'YY-M-D / HH:mm:ss' expect(dayjs().format(string)).toBe(moment().format(string)) diff --git a/test/locale.th.test.js b/test/locale.th.test.js deleted file mode 100644 index 13d917890..000000000 --- a/test/locale.th.test.js +++ /dev/null @@ -1,99 +0,0 @@ -import MockDate from 'mockdate' -import dayjs from '../src' -import th from '../src/locale/th' - -beforeEach(() => { - MockDate.set(new Date()) -}) - -afterEach(() => { - MockDate.reset() -}) - -const format = 'dddd D, MMMM' - -it('Uses Thai locale through constructor', () => { // not recommend - expect(dayjs('2018-4-28', { locale: th }) - .format(format)) - .toBe('เสาร์ 28, เมษายน') - expect(dayjs('2018-4-28', { locale: th }) - .format('ddd D, MMM')) - .toBe('ส. 28, เม.ย.') -}) - -it('set locale for one instance only', () => { - expect(dayjs('2018-4-28') - .format(format)) - .toBe('Saturday 28, April') - - expect(dayjs('2018-4-28') - .locale(th).format(format)) - .toBe('เสาร์ 28, เมษายน') - - expect(dayjs('2018-4-28') - .format(format)) - .toBe('Saturday 28, April') -}) - -it('set locale for this line only', () => { - expect(dayjs('2018-4-28').format(format, th)) - .toBe('เสาร์ 28, เมษายน') -}) - -it('set global locale', () => { - dayjs.locale('en') - expect(dayjs('2018-4-28').format(format)) - .toBe('Saturday 28, April') - dayjs.locale(th) - expect(dayjs('2018-4-28').format(format)) - .toBe('เสาร์ 28, เมษายน') - dayjs.locale('en') - expect(dayjs('2018-4-28').format(format)) - .toBe('Saturday 28, April') -}) - -it('immutable instance locale', () => { - dayjs.locale('en') - const origin = dayjs('2018-4-28') - expect(origin.format(format)) - .toBe('Saturday 28, April') - expect(origin.locale('th').format(format)) - .toBe('เสาร์ 28, เมษายน') - const changed = origin.locale('th') - expect(changed.format(format)) - .toBe('เสาร์ 28, เมษายน') - expect(origin.format(format)) - .toBe('Saturday 28, April') -}) - -describe('Instance locale inheritance', () => { - const thDayjs = dayjs('2018-4-28').locale(th) - - it('Clone', () => { - expect(thDayjs.clone().format(format)) - .toBe('เสาร์ 28, เมษายน') - expect(dayjs(thDayjs).format(format)) - .toBe('เสาร์ 28, เมษายน') - }) - - it('StartOf EndOf', () => { - expect(thDayjs.startOf('year').format(format)) - .toBe('จันทร์ 1, มกราคม') - expect(thDayjs.endOf('day').format(format)) - .toBe('เสาร์ 28, เมษายน') - }) - - it('Set', () => { - expect(thDayjs.set('year', 2017).format(format)) - .toBe('ศุกร์ 28, เมษายน') - }) - - it('Add', () => { - expect(thDayjs.add(1, 'year').format(format)) - .toBe('อาทิตย์ 28, เมษายน') - expect(thDayjs.add(1, 'month').format(format)) - .toBe('จันทร์ 28, พฤษภาคม') - expect(thDayjs.add(1, 'minute').format(format)) - .toBe('เสาร์ 28, เมษายน') - }) -}) From 3e2b6f51965fda002ec01be2b0097910bc71f3c6 Mon Sep 17 00:00:00 2001 From: iamkun Date: Wed, 6 Jun 2018 14:49:19 +0800 Subject: [PATCH 02/10] docs: update format API --- docs/en/API-reference.md | 1 + docs/ja/API-reference.md | 1 + docs/ko/API-reference.md | 1 + docs/pt-br/API-reference.md | 1 + docs/zh-cn/API-reference.md | 1 + 5 files changed, 5 insertions(+) diff --git a/docs/en/API-reference.md b/docs/en/API-reference.md index 1296f59fc..065be822b 100644 --- a/docs/en/API-reference.md +++ b/docs/en/API-reference.md @@ -241,6 +241,7 @@ dayjs('2019-01-25').format('DD/MM/YYYY'); // '25/01/2019' | `D` | 1-31 | The day of the month | | `DD` | 01-31 | The day of the month, 2-digits | | `d` | 0-6 | The day of the week, with Sunday as 0 | +| `ddd` | Sun-Sat | The short name of the day of the week | | `dddd` | Sunday-Saturday | The name of the day of the week | | `H` | 0-23 | The hour | | `HH` | 00-23 | The hour, 2-digits | diff --git a/docs/ja/API-reference.md b/docs/ja/API-reference.md index f86061e87..3ab616f29 100644 --- a/docs/ja/API-reference.md +++ b/docs/ja/API-reference.md @@ -290,6 +290,7 @@ dayjs().format('{YYYY} MM-DDTHH:mm:ssZ[Z]'); // "{2014} 09-08T08:02:17-05:00Z" | `D` | 1-31 | 日 | | `DD` | 01-31 | 2桁の日 | | `d` | 0-6 | 曜日 (日曜は0) | +| `ddd` | Sun-Sat | 曜日の略称 | | `dddd` | Sunday-Saturday | 曜日名 | | `H` | 0-23 | 時間 | | `HH` | 00-23 | 2桁の時間 | diff --git a/docs/ko/API-reference.md b/docs/ko/API-reference.md index f7373a0c6..e082f4789 100644 --- a/docs/ko/API-reference.md +++ b/docs/ko/API-reference.md @@ -241,6 +241,7 @@ dayjs('2019-01-25').format('DD/MM/YYYY'); // '25/01/2019' | `D` | 1-31 | 일 | | `DD` | 01-31 | 일, 두 자리로 표현 | | `d` | 0-6 | 요일, 일요일은 0 | +| `ddd` | Sun-Sat | The short name of the day of the week | | `dddd` | Sunday-Saturday | 요일 이름 | | `H` | 0-23 | 시간 | | `HH` | 00-23 | 시간, 두 자리로 표현 | diff --git a/docs/pt-br/API-reference.md b/docs/pt-br/API-reference.md index ec54d62b2..583e680c0 100644 --- a/docs/pt-br/API-reference.md +++ b/docs/pt-br/API-reference.md @@ -271,6 +271,7 @@ Lista de todos os formatos disponíveis: | `D` | 1-31 | Dia do mês | | `DD` | 01-31 | Dia do mês, com 2 dígitos | | `d` | 0-6 | Dia da semana (Domingo = 0) | +| `ddd` | Sun-Sat | The short name of the day of the week | | `dddd` | Sunday-Saturday | Nome do dia da semana | | `H` | 0-23 | Hora | | `HH` | 00-23 | Hora, com 2 dígitos | diff --git a/docs/zh-cn/API-reference.md b/docs/zh-cn/API-reference.md index 525194024..c0e571e80 100644 --- a/docs/zh-cn/API-reference.md +++ b/docs/zh-cn/API-reference.md @@ -212,6 +212,7 @@ dayjs().format("{YYYY} MM-DDTHH:mm:ssZ[Z]"); // "{2014} 09-08T08:02:17-05:00Z" | `D` | 1-31 | 月份里的一天 | | `DD` | 01-31 | 月份里的一天,两位数 | | `d` | 0-6 | 一周中的一天,星期天是 0 | +| `ddd` | Sun-Sat | 简写的一周中一天的名称 | | `dddd` | Sunday-Saturday | 一周中一天的名称 | | `H` | 0-23 | 小时 | | `HH` | 00-23 | 小时,两位数 | From da305e33d5032e00b2a634e8000e050d854f962b Mon Sep 17 00:00:00 2001 From: iamkun Date: Wed, 6 Jun 2018 14:50:50 +0800 Subject: [PATCH 03/10] test: update short locale test --- test/locale/keys.test.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/test/locale/keys.test.js b/test/locale/keys.test.js index 16e21f650..8d5dcef15 100644 --- a/test/locale/keys.test.js +++ b/test/locale/keys.test.js @@ -15,10 +15,20 @@ fs.readdirSync(path.join(__dirname, localeDir)) it('Locale keys', () => { L.forEach((l) => { const { - name, ordinal, weekdays, months, relativeTime + name, + ordinal, + weekdays, + months, + relativeTime, + weekdaysShort, + monthsShort } = l expect(name).toEqual(expect.any(String)) expect(weekdays).toEqual(expect.any(Array)) + + if (weekdaysShort) expect(weekdaysShort).toEqual(expect.any(Array)) + if (monthsShort) expect(monthsShort).toEqual(expect.any(Array)) + expect(months).toEqual(expect.any(Array)) // function pass date return string or number or null expect(ordinal(1)).toEqual(expect.anything()) From ac151dc717ec6d52fe8270469b337846351413d3 Mon Sep 17 00:00:00 2001 From: iamkun Date: Wed, 6 Jun 2018 14:55:23 +0800 Subject: [PATCH 04/10] docs: update i18n docs --- docs/en/I18n.md | 2 ++ docs/ja/I18n.md | 2 ++ docs/ko/I18n.md | 2 ++ docs/pt-br/I18n.md | 2 ++ docs/zh-cn/I18n.md | 2 ++ 5 files changed, 10 insertions(+) diff --git a/docs/en/I18n.md b/docs/en/I18n.md index 8cafae4a1..250e7e2d4 100644 --- a/docs/en/I18n.md +++ b/docs/en/I18n.md @@ -78,7 +78,9 @@ Template of a Day.js locale Object. const localeObject = { name: 'es', // name String weekdays: 'Domingo_Lunes ...'.split('_'), // weekdays Array + weekdaysShort: 'Sun_M'.split('_'), // OPTIONAL, short weekdays Array, use first three letters if not provided months: 'Enero_Febrero ... '.split('_'), // months Array + monthsShort: 'Jan_F'.split('_'), // OPTIONAL, short months Array, use first three letters if not provided 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 diff --git a/docs/ja/I18n.md b/docs/ja/I18n.md index 9aaecc90a..e08b7030e 100644 --- a/docs/ja/I18n.md +++ b/docs/ja/I18n.md @@ -80,7 +80,9 @@ dayjs().locale('es').format() // ロケールを特定のインスタンスに const localeObject = { name: 'es', // ロケール名を表す文字列 weekdays: 'Domingo_Lunes ...'.split('_'), // 曜日の配列 + weekdaysShort: 'Sun_M'.split('_'), // OPTIONAL, short weekdays Array, use first three letters if not provided months: 'Enero_Febrero ... '.split('_'), // 月の配列 + monthsShort: 'Jan_F'.split('_'), // OPTIONAL, short months Array, use first three letters if not provided 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 diff --git a/docs/ko/I18n.md b/docs/ko/I18n.md index 3fc5ddead..28a38836d 100644 --- a/docs/ko/I18n.md +++ b/docs/ko/I18n.md @@ -78,7 +78,9 @@ Day.js locale 오브젝트 템플릿 입니다. const localeObject = { name: 'es', // name String weekdays: 'Domingo_Lunes ...'.split('_'), // weekdays Array + weekdaysShort: 'Sun_M'.split('_'), // OPTIONAL, short weekdays Array, use first three letters if not provided months: 'Enero_Febrero ... '.split('_'), // months Array + monthsShort: 'Jan_F'.split('_'), // OPTIONAL, short months Array, use first three letters if not provided 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 diff --git a/docs/pt-br/I18n.md b/docs/pt-br/I18n.md index 673d3e21b..2f55b677c 100644 --- a/docs/pt-br/I18n.md +++ b/docs/pt-br/I18n.md @@ -73,7 +73,9 @@ Template de um objeto de locale do Day.js. const objetoLocale = { name: 'es', // name: String weekdays: 'Domingo_Lunes ...'.split('_'), // weekdays: Array + weekdaysShort: 'Sun_M'.split('_'), // OPTIONAL, short weekdays Array, use first three letters if not provided months: 'Enero_Febrero ... '.split('_'), // months: Array + monthsShort: 'Jan_F'.split('_'), // OPTIONAL, short months Array, use first three letters if not provided 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 diff --git a/docs/zh-cn/I18n.md b/docs/zh-cn/I18n.md index 8aff8d9dc..6d7b38e95 100644 --- a/docs/zh-cn/I18n.md +++ b/docs/zh-cn/I18n.md @@ -78,7 +78,9 @@ Day.js 的语言配置模版 const localeObject = { name: 'es', // 语言名 String weekdays: 'Domingo_Lunes ...'.split('_'), // 星期 Array + weekdaysShort: 'Sun_M'.split('_'), // 可选, 短的星期 Array, 如果没提供则使用前三个字符 months: 'Enero_Febrero ... '.split('_'), // 月份 Array + monthsShort: 'Jan_F'.split('_'), // 可选, 短的月份 Array, 如果没提供则使用前三个字符 ordinal: n => `${n}º`, // 序号生成工厂函数 Function (number) => return number + output relativeTime: { // 相对时间, %s %d 不用翻译 future: 'in %s', // e.g. in 2 hours, %s been replaced with 2hours From 96a8da2c1133f24934d21488d7c2fdd7f560d43a Mon Sep 17 00:00:00 2001 From: iamkun Date: Wed, 6 Jun 2018 15:16:10 +0800 Subject: [PATCH 05/10] chore: add dd format --- src/index.js | 15 ++++++++++----- src/locale/th.js | 1 + src/locale/zh-cn.js | 3 +++ test/display.test.js | 4 +++- test/locale.test.js | 5 ----- test/locale/keys.test.js | 4 +++- 6 files changed, 20 insertions(+), 12 deletions(-) diff --git a/src/index.js b/src/index.js index d396caa23..8671018ae 100644 --- a/src/index.js +++ b/src/index.js @@ -274,13 +274,16 @@ class Dayjs { } - format(formatStr, localeObject) { + format(formatStr) { const str = formatStr || C.FORMAT_DEFAULT const zoneStr = Utils.padZoneStr(this.$d.getTimezoneOffset()) - const locale = localeObject || this.$locale() + const locale = this.$locale() const { - weekdays, months, weekdaysShort, monthsShort + weekdays, months } = locale + const getShort = (arr, index, full, length) => ( + (arr && arr[index]) || full[index].substr(0, length) + ) return str.replace(C.REGEX_FORMAT, (match) => { if (match.indexOf('[') > -1) return match.replace(/\[|\]/g, '') switch (match) { @@ -293,7 +296,7 @@ class Dayjs { case 'MM': return Utils.padStart(this.$M + 1, 2, '0') case 'MMM': - return (monthsShort && monthsShort[this.$M]) || months[this.$M].slice(0, 3) + return getShort(locale.monthsShort, this.$M, months, 3) case 'MMMM': return months[this.$M] case 'D': @@ -302,8 +305,10 @@ class Dayjs { return Utils.padStart(this.$D, 2, '0') case 'd': return String(this.$W) + case 'dd': + return getShort(locale.weekdaysMin, this.$W, weekdays, 2) case 'ddd': - return (weekdaysShort && weekdaysShort[this.$W]) || weekdays[this.$W].substr(0, 3) + return getShort(locale.weekdaysShort, this.$W, weekdays, 3) case 'dddd': return weekdays[this.$W] case 'H': diff --git a/src/locale/th.js b/src/locale/th.js index 31d7b09f2..a9cc8fc52 100644 --- a/src/locale/th.js +++ b/src/locale/th.js @@ -4,6 +4,7 @@ const locale = { name: 'th', weekdays: 'อาทิตย์_จันทร์_อังคาร_พุธ_พฤหัสบดี_ศุกร์_เสาร์'.split('_'), weekdaysShort: 'อาทิตย์_จันทร์_อังคาร_พุธ_พฤหัส_ศุกร์_เสาร์'.split('_'), + weekdaysMin: 'อา._จ._อ._พ._พฤ._ศ._ส.'.split('_'), months: 'มกราคม_กุมภาพันธ์_มีนาคม_เมษายน_พฤษภาคม_มิถุนายน_กรกฏาคม_สิงหาคม_กันยายน_ตุลาคม_พฤศจิกายน_ธันวาคม'.split('_'), monthsShort: 'ม.ค._ก.พ._มี.ค._เม.ย._พ.ค._มิ.ย._ก.ค._ส.ค._ก.ย._ต.ค._พ.ย._ธ.ค.'.split('_'), relativeTime: { diff --git a/src/locale/zh-cn.js b/src/locale/zh-cn.js index 29c79e11c..57114bac7 100644 --- a/src/locale/zh-cn.js +++ b/src/locale/zh-cn.js @@ -3,7 +3,10 @@ import dayjs from 'dayjs' const locale = { name: 'zh-cn', weekdays: '星期日_星期一_星期二_星期三_星期四_星期五_星期六'.split('_'), + weekdaysShort: '周日_周一_周二_周三_周四_周五_周六'.split('_'), + weekdaysMin: '日_一_二_三_四_五_六'.split('_'), months: '一月_二月_三月_四月_五月_六月_七月_八月_九月_十月_十一月_十二月'.split('_'), + monthsShort: '1月_2月_3月_4月_5月_6月_7月_8月_9月_10月_11月_12月'.split('_'), ordinal: n => n, relativeTime: { future: '%s内', diff --git a/test/display.test.js b/test/display.test.js index dc5c72d65..29024b9f3 100644 --- a/test/display.test.js +++ b/test/display.test.js @@ -34,6 +34,7 @@ it('Format Day of Month D DD 1 - 31', () => { it('Format Day of Week d Sun - Sat', () => { expect(dayjs().format('d')).toBe(moment().format('d')) + expect(dayjs().format('dd')).toBe(moment().format('dd')) expect(dayjs().format('ddd')).toBe(moment().format('ddd')) expect(dayjs().format('dddd')).toBe(moment().format('dddd')) }) @@ -97,7 +98,8 @@ it('Format Time Zone ZZ', () => { expect(dayjs().format('ZZ')).toBe(moment().format('ZZ')) }) -it('Format ddd MMM with short locale', () => { +it('Format ddd dd MMM with short locale', () => { + expect(dayjs().locale(th).format('dd')).toBe(moment().locale('th').format('dd')) expect(dayjs().locale(th).format('ddd')).toBe(moment().locale('th').format('ddd')) expect(dayjs().locale(th).format('MMM')).toBe(moment().locale('th').format('MMM')) }) diff --git a/test/locale.test.js b/test/locale.test.js index 5570a062c..c1daa8dad 100644 --- a/test/locale.test.js +++ b/test/locale.test.js @@ -32,11 +32,6 @@ it('set locale for one instance only', () => { .toBe('Saturday 28, April') }) -it('set locale for this line only', () => { - expect(dayjs('2018-4-28').format(format, es)) - .toBe('Sábado 28, Abril') -}) - it('set global locale', () => { dayjs.locale('en') expect(dayjs('2018-4-28').format(format)) diff --git a/test/locale/keys.test.js b/test/locale/keys.test.js index 8d5dcef15..68219c751 100644 --- a/test/locale/keys.test.js +++ b/test/locale/keys.test.js @@ -21,13 +21,15 @@ it('Locale keys', () => { months, relativeTime, weekdaysShort, - monthsShort + monthsShort, + weekdaysMin } = l expect(name).toEqual(expect.any(String)) expect(weekdays).toEqual(expect.any(Array)) if (weekdaysShort) expect(weekdaysShort).toEqual(expect.any(Array)) if (monthsShort) expect(monthsShort).toEqual(expect.any(Array)) + if (weekdaysMin) expect(weekdaysMin).toEqual(expect.any(Array)) expect(months).toEqual(expect.any(Array)) // function pass date return string or number or null From 07dbc58808741c64c6adee94ab63b9a0d30c5300 Mon Sep 17 00:00:00 2001 From: iamkun Date: Wed, 6 Jun 2018 15:21:05 +0800 Subject: [PATCH 06/10] docs: update docs --- docs/en/API-reference.md | 1 + docs/en/I18n.md | 1 + docs/ja/API-reference.md | 1 + docs/ja/I18n.md | 1 + docs/ko/API-reference.md | 1 + docs/ko/I18n.md | 1 + docs/pt-br/API-reference.md | 1 + docs/pt-br/I18n.md | 1 + docs/zh-cn/API-reference.md | 1 + docs/zh-cn/I18n.md | 1 + 10 files changed, 10 insertions(+) diff --git a/docs/en/API-reference.md b/docs/en/API-reference.md index 065be822b..4d1c1b896 100644 --- a/docs/en/API-reference.md +++ b/docs/en/API-reference.md @@ -241,6 +241,7 @@ dayjs('2019-01-25').format('DD/MM/YYYY'); // '25/01/2019' | `D` | 1-31 | The day of the month | | `DD` | 01-31 | The day of the month, 2-digits | | `d` | 0-6 | The day of the week, with Sunday as 0 | +| `dd` | Su-Sa | The min name of the day of the week | | `ddd` | Sun-Sat | The short name of the day of the week | | `dddd` | Sunday-Saturday | The name of the day of the week | | `H` | 0-23 | The hour | diff --git a/docs/en/I18n.md b/docs/en/I18n.md index 250e7e2d4..c08a1a1b9 100644 --- a/docs/en/I18n.md +++ b/docs/en/I18n.md @@ -79,6 +79,7 @@ const localeObject = { name: 'es', // name String weekdays: 'Domingo_Lunes ...'.split('_'), // weekdays Array weekdaysShort: 'Sun_M'.split('_'), // OPTIONAL, short weekdays Array, use first three letters if not provided + weekdaysMin: 'Su_Mo'.split('_'), // OPTIONAL, min weekdays Array, use first two letters if not provided months: 'Enero_Febrero ... '.split('_'), // months Array monthsShort: 'Jan_F'.split('_'), // OPTIONAL, short months Array, use first three letters if not provided ordinal: n => `${n}º`, // ordinal Function (number) => return number + output diff --git a/docs/ja/API-reference.md b/docs/ja/API-reference.md index 3ab616f29..f609af937 100644 --- a/docs/ja/API-reference.md +++ b/docs/ja/API-reference.md @@ -290,6 +290,7 @@ dayjs().format('{YYYY} MM-DDTHH:mm:ssZ[Z]'); // "{2014} 09-08T08:02:17-05:00Z" | `D` | 1-31 | 日 | | `DD` | 01-31 | 2桁の日 | | `d` | 0-6 | 曜日 (日曜は0) | +| `dd` | Su-Sa | The min name of the day of the week | | `ddd` | Sun-Sat | 曜日の略称 | | `dddd` | Sunday-Saturday | 曜日名 | | `H` | 0-23 | 時間 | diff --git a/docs/ja/I18n.md b/docs/ja/I18n.md index e08b7030e..b6cc54453 100644 --- a/docs/ja/I18n.md +++ b/docs/ja/I18n.md @@ -81,6 +81,7 @@ const localeObject = { name: 'es', // ロケール名を表す文字列 weekdays: 'Domingo_Lunes ...'.split('_'), // 曜日の配列 weekdaysShort: 'Sun_M'.split('_'), // OPTIONAL, short weekdays Array, use first three letters if not provided + weekdaysMin: 'Su_Mo'.split('_'), // OPTIONAL, min weekdays Array, use first two letters if not provided months: 'Enero_Febrero ... '.split('_'), // 月の配列 monthsShort: 'Jan_F'.split('_'), // OPTIONAL, short months Array, use first three letters if not provided ordinal: n => `${n}º`, // 序数 Function (number) => return number + output diff --git a/docs/ko/API-reference.md b/docs/ko/API-reference.md index e082f4789..729d10ea4 100644 --- a/docs/ko/API-reference.md +++ b/docs/ko/API-reference.md @@ -241,6 +241,7 @@ dayjs('2019-01-25').format('DD/MM/YYYY'); // '25/01/2019' | `D` | 1-31 | 일 | | `DD` | 01-31 | 일, 두 자리로 표현 | | `d` | 0-6 | 요일, 일요일은 0 | +| `dd` | Su-Sa | The min name of the day of the week | | `ddd` | Sun-Sat | The short name of the day of the week | | `dddd` | Sunday-Saturday | 요일 이름 | | `H` | 0-23 | 시간 | diff --git a/docs/ko/I18n.md b/docs/ko/I18n.md index 28a38836d..fef40a6ee 100644 --- a/docs/ko/I18n.md +++ b/docs/ko/I18n.md @@ -79,6 +79,7 @@ const localeObject = { name: 'es', // name String weekdays: 'Domingo_Lunes ...'.split('_'), // weekdays Array weekdaysShort: 'Sun_M'.split('_'), // OPTIONAL, short weekdays Array, use first three letters if not provided + weekdaysMin: 'Su_Mo'.split('_'), // OPTIONAL, min weekdays Array, use first two letters if not provided months: 'Enero_Febrero ... '.split('_'), // months Array monthsShort: 'Jan_F'.split('_'), // OPTIONAL, short months Array, use first three letters if not provided ordinal: n => `${n}º`, // ordinal Function (number) => return number + output diff --git a/docs/pt-br/API-reference.md b/docs/pt-br/API-reference.md index 583e680c0..060f7a278 100644 --- a/docs/pt-br/API-reference.md +++ b/docs/pt-br/API-reference.md @@ -271,6 +271,7 @@ Lista de todos os formatos disponíveis: | `D` | 1-31 | Dia do mês | | `DD` | 01-31 | Dia do mês, com 2 dígitos | | `d` | 0-6 | Dia da semana (Domingo = 0) | +| `dd` | Su-Sa | The min name of the day of the week | | `ddd` | Sun-Sat | The short name of the day of the week | | `dddd` | Sunday-Saturday | Nome do dia da semana | | `H` | 0-23 | Hora | diff --git a/docs/pt-br/I18n.md b/docs/pt-br/I18n.md index 2f55b677c..1b62b3f62 100644 --- a/docs/pt-br/I18n.md +++ b/docs/pt-br/I18n.md @@ -74,6 +74,7 @@ const objetoLocale = { name: 'es', // name: String weekdays: 'Domingo_Lunes ...'.split('_'), // weekdays: Array weekdaysShort: 'Sun_M'.split('_'), // OPTIONAL, short weekdays Array, use first three letters if not provided + weekdaysMin: 'Su_Mo'.split('_'), // OPTIONAL, min weekdays Array, use first two letters if not provided months: 'Enero_Febrero ... '.split('_'), // months: Array monthsShort: 'Jan_F'.split('_'), // OPTIONAL, short months Array, use first three letters if not provided ordinal: n => `${n}º`, // ordinal: Function (number) => return number + saída diff --git a/docs/zh-cn/API-reference.md b/docs/zh-cn/API-reference.md index c0e571e80..5bacb1e7a 100644 --- a/docs/zh-cn/API-reference.md +++ b/docs/zh-cn/API-reference.md @@ -212,6 +212,7 @@ dayjs().format("{YYYY} MM-DDTHH:mm:ssZ[Z]"); // "{2014} 09-08T08:02:17-05:00Z" | `D` | 1-31 | 月份里的一天 | | `DD` | 01-31 | 月份里的一天,两位数 | | `d` | 0-6 | 一周中的一天,星期天是 0 | +| `dd` | Su-Sa | 最简写的一周中一天的名称 | | `ddd` | Sun-Sat | 简写的一周中一天的名称 | | `dddd` | Sunday-Saturday | 一周中一天的名称 | | `H` | 0-23 | 小时 | diff --git a/docs/zh-cn/I18n.md b/docs/zh-cn/I18n.md index 6d7b38e95..e71833831 100644 --- a/docs/zh-cn/I18n.md +++ b/docs/zh-cn/I18n.md @@ -79,6 +79,7 @@ const localeObject = { name: 'es', // 语言名 String weekdays: 'Domingo_Lunes ...'.split('_'), // 星期 Array weekdaysShort: 'Sun_M'.split('_'), // 可选, 短的星期 Array, 如果没提供则使用前三个字符 + weekdaysMin: 'Su_Mo'.split('_'), // 可选, 最短的星期 Array, 如果没提供则使用前两个字符 months: 'Enero_Febrero ... '.split('_'), // 月份 Array monthsShort: 'Jan_F'.split('_'), // 可选, 短的月份 Array, 如果没提供则使用前三个字符 ordinal: n => `${n}º`, // 序号生成工厂函数 Function (number) => return number + output From 56f324fb7a6077580dbae20f7f753df8de81d4a4 Mon Sep 17 00:00:00 2001 From: iamkun Date: Wed, 6 Jun 2018 15:27:06 +0800 Subject: [PATCH 07/10] refactor: isLeapYear as plugin --- src/plugin/isLeapYear/index.js | 7 +++++++ test/plugin/isLeapYear.test.js | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 src/plugin/isLeapYear/index.js create mode 100644 test/plugin/isLeapYear.test.js diff --git a/src/plugin/isLeapYear/index.js b/src/plugin/isLeapYear/index.js new file mode 100644 index 000000000..35df7b46e --- /dev/null +++ b/src/plugin/isLeapYear/index.js @@ -0,0 +1,7 @@ +export default (o, c) => { + const proto = c.prototype + proto.isLeapYear = function () { + return ((this.$y % 4 === 0) && (this.$y % 100 !== 0)) || (this.$y % 400 === 0) + } +} + diff --git a/test/plugin/isLeapYear.test.js b/test/plugin/isLeapYear.test.js new file mode 100644 index 000000000..746fb9e0f --- /dev/null +++ b/test/plugin/isLeapYear.test.js @@ -0,0 +1,18 @@ +import MockDate from 'mockdate' +import dayjs from '../../src' +import isLeapYear from '../../src/plugin/isLeapYear' + +dayjs.extend(isLeapYear) + +beforeEach(() => { + MockDate.set(new Date()) +}) + +afterEach(() => { + MockDate.reset() +}) + +it('IsLeapYear', () => { + expect(dayjs('20000101').isLeapYear()).toBe(true) + expect(dayjs('2100-01-01').isLeapYear()).toBe(false) +}) From 28d04bf3667b04106764fa820260a188a4421557 Mon Sep 17 00:00:00 2001 From: iamkun Date: Wed, 6 Jun 2018 15:34:25 +0800 Subject: [PATCH 08/10] docs: isLeapYear doc update --- docs/en/Plugin.md | 10 ++++++++++ docs/ja/Plugin.md | 10 ++++++++++ docs/ko/Plugin.md | 10 ++++++++++ docs/pt-br/Plugin.md | 10 ++++++++++ docs/zh-cn/Plugin.md | 11 +++++++++++ 5 files changed, 51 insertions(+) diff --git a/docs/en/Plugin.md b/docs/en/Plugin.md index c76f33182..32b903e24 100644 --- a/docs/en/Plugin.md +++ b/docs/en/Plugin.md @@ -113,6 +113,16 @@ Returns the `string` of relative time to X. | 11 months to 17months | y | a year ago | | 18 months+ | yy | 2 years ago ... 20 years ago | +### IsLeapYear + - IsLeapYear adds `.isLeapYear` API to returns a `boolean` indicating whether the `Dayjs`'s year is a leap year or not. + +```javascript +import isLeapYear from 'dayjs/plugin/isLeapYear' + +dayjs.extend(isLeapYear) + +dayjs('2000-01-01').isLeapYear(); // true +``` ## Customize diff --git a/docs/ja/Plugin.md b/docs/ja/Plugin.md index 1c3836f1e..dadeb2d2c 100644 --- a/docs/ja/Plugin.md +++ b/docs/ja/Plugin.md @@ -114,6 +114,16 @@ Returns the `string` of relative time to X. | 11 months to 17months | y | a year ago | | 18 months+ | yy | 2 years ago ... 20 years ago | +### IsLeapYear + - IsLeapYear adds `.isLeapYear` API to returns a `boolean` indicating whether the `Dayjs`'s year is a leap year or not. + +```javascript +import isLeapYear from 'dayjs/plugin/isLeapYear' + +dayjs.extend(isLeapYear) + +dayjs('2000-01-01').isLeapYear(); // true +``` ## カスタマイズ diff --git a/docs/ko/Plugin.md b/docs/ko/Plugin.md index 4d40f7644..64118ca92 100644 --- a/docs/ko/Plugin.md +++ b/docs/ko/Plugin.md @@ -113,6 +113,16 @@ X 시간부터 상대시간을 `string`으로 반환합니다. | 11 달 ~ 17 달 | y | 일년 전 | | 18 달 이상 | yy | 2 년 전 ~ 20 년 전 | +### IsLeapYear + - IsLeapYear adds `.isLeapYear` API to returns a `boolean` indicating whether the `Dayjs`'s year is a leap year or not. + +```javascript +import isLeapYear from 'dayjs/plugin/isLeapYear' + +dayjs.extend(isLeapYear) + +dayjs('2000-01-01').isLeapYear(); // true +``` ## Customize diff --git a/docs/pt-br/Plugin.md b/docs/pt-br/Plugin.md index f67d2c67e..2ab777c29 100644 --- a/docs/pt-br/Plugin.md +++ b/docs/pt-br/Plugin.md @@ -113,6 +113,16 @@ Returns the `string` of relative time to X. | 11 months to 17months | y | a year ago | | 18 months+ | yy | 2 years ago ... 20 years ago | +### IsLeapYear + - IsLeapYear adds `.isLeapYear` API to returns a `boolean` indicating whether the `Dayjs`'s year is a leap year or not. + +```javascript +import isLeapYear from 'dayjs/plugin/isLeapYear' + +dayjs.extend(isLeapYear) + +dayjs('2000-01-01').isLeapYear(); // true +``` ## Customizar diff --git a/docs/zh-cn/Plugin.md b/docs/zh-cn/Plugin.md index 810d2f2c9..0dd041046 100644 --- a/docs/zh-cn/Plugin.md +++ b/docs/zh-cn/Plugin.md @@ -113,6 +113,17 @@ dayjs().toNow() | 11 月 到 17月 | y | 1 年前 | | 18 月以上 | yy | 2 年前 ... 20 年前 | +## IsLeapYear + - IsLeapYear 增加了 `.isLeapYear` API 返回一个 `boolean` 来展示一个 `Dayjs`'s 的年份是不是闰年. + +```javascript +import isLeapYear from 'dayjs/plugin/isLeapYear' + +dayjs.extend(isLeapYear) + +dayjs('2000-01-01').isLeapYear(); // true +``` + ## 自定义 你可以根据需要自由的编写一个Day.js插件 From 392c0353db2d69fb967059f242a6dd0b7df55a58 Mon Sep 17 00:00:00 2001 From: iamkun Date: Wed, 6 Jun 2018 15:36:19 +0800 Subject: [PATCH 09/10] docs: update api doc --- docs/en/API-reference.md | 6 ++++++ docs/ja/API-reference.md | 6 ++++++ docs/ko/API-reference.md | 6 ++++++ docs/pt-br/API-reference.md | 6 ++++++ docs/zh-cn/API-reference.md | 8 +++++++- 5 files changed, 31 insertions(+), 1 deletion(-) diff --git a/docs/en/API-reference.md b/docs/en/API-reference.md index 4d1c1b896..cc9b725b9 100644 --- a/docs/en/API-reference.md +++ b/docs/en/API-reference.md @@ -393,3 +393,9 @@ dayjs('2000-01-01').isLeapYear(); // true `.from` `.to` `.fromNow` `.toNow` to get relative time plugin [`RelativeTime`](./Plugin.md#relativetime) + +### IsLeapYear + +`.isLeapYear` to get is a leap year or not + +plugin [`IsLeapYear`](./Plugin.md#isleapyear) diff --git a/docs/ja/API-reference.md b/docs/ja/API-reference.md index f609af937..f5a3177f7 100644 --- a/docs/ja/API-reference.md +++ b/docs/ja/API-reference.md @@ -466,3 +466,9 @@ dayjs('2000-01-01').isLeapYear(); // true `.from` `.to` `.fromNow` `.toNow` to get relative time plugin [`RelativeTime`](./Plugin.md#relativetime) + +### IsLeapYear + +`.isLeapYear` to get is a leap year or not + +plugin [`IsLeapYear`](./Plugin.md#isleapyear) diff --git a/docs/ko/API-reference.md b/docs/ko/API-reference.md index 729d10ea4..8d914b45d 100644 --- a/docs/ko/API-reference.md +++ b/docs/ko/API-reference.md @@ -393,3 +393,9 @@ dayjs('2000-01-01').isLeapYear(); // true `.from` `.to` `.fromNow` `.toNow`에 대한 상대 시간을 가져옵니다. 플러그인 [`RelativeTime`](./Plugin.md#relativetime) + +### IsLeapYear + +`.isLeapYear` to get is a leap year or not + +plugin [`IsLeapYear`](./Plugin.md#isleapyear) diff --git a/docs/pt-br/API-reference.md b/docs/pt-br/API-reference.md index 060f7a278..8859c5f68 100644 --- a/docs/pt-br/API-reference.md +++ b/docs/pt-br/API-reference.md @@ -444,3 +444,9 @@ dayjs('2000-01-01').isLeapYear(); // true `.from` `.to` `.fromNow` `.toNow` to get relative time plugin [`RelativeTime`](./Plugin.md#relativetime) + +### IsLeapYear + +`.isLeapYear` to get is a leap year or not + +plugin [`IsLeapYear`](./Plugin.md#isleapyear) diff --git a/docs/zh-cn/API-reference.md b/docs/zh-cn/API-reference.md index 5bacb1e7a..698845158 100644 --- a/docs/zh-cn/API-reference.md +++ b/docs/zh-cn/API-reference.md @@ -340,4 +340,10 @@ dayjs('2000-01-01').isLeapYear(); // true `.from` `.to` `.fromNow` `.toNow` 获得相对时间 -插件 [`RelativeTime`](./Plugin.md#relativetime) \ No newline at end of file +插件 [`RelativeTime`](./Plugin.md#relativetime) + +### IsLeapYear + +`.isLeapYear` 获得是否闰年 + +插件 [`IsLeapYear`](./Plugin.md#isleapyear) \ No newline at end of file From d37f4563406ea9d9a5344cc0d1ed54f624da5e03 Mon Sep 17 00:00:00 2001 From: iamkun Date: Wed, 6 Jun 2018 15:39:19 +0800 Subject: [PATCH 10/10] docs: add DEPRECATED note --- docs/en/API-reference.md | 2 ++ docs/ja/API-reference.md | 2 ++ docs/ko/API-reference.md | 2 ++ docs/pt-br/API-reference.md | 2 ++ docs/zh-cn/API-reference.md | 3 +++ 5 files changed, 11 insertions(+) diff --git a/docs/en/API-reference.md b/docs/en/API-reference.md index cc9b725b9..784a83288 100644 --- a/docs/en/API-reference.md +++ b/docs/en/API-reference.md @@ -380,6 +380,8 @@ dayjs().isAfter(dayjs()); // false ### Is Leap Year `.isLeapYear()` +**[DEPRECATED] in 1.7.0, use [`IsLeapYear plugin`](./Plugin.md#isleapyear) instead** + Returns a `boolean` indicating whether the `Dayjs`'s year is a leap year or not. ```js diff --git a/docs/ja/API-reference.md b/docs/ja/API-reference.md index f5a3177f7..dc73130ee 100644 --- a/docs/ja/API-reference.md +++ b/docs/ja/API-reference.md @@ -450,6 +450,8 @@ dayjs().isAfter(dayjs()); // false #### Is Leap Year +**[DEPRECATED] in 1.7.0, use [`IsLeapYear plugin`](./Plugin.md#isleapyear) instead** + * Boolean を返します その年がうるう年かどうかをチェックします。 diff --git a/docs/ko/API-reference.md b/docs/ko/API-reference.md index 8d914b45d..b875672dc 100644 --- a/docs/ko/API-reference.md +++ b/docs/ko/API-reference.md @@ -380,6 +380,8 @@ dayjs().isAfter(dayjs()); // false ### Is Leap Year `.isLeapYear()` +**[DEPRECATED] in 1.7.0, use [`IsLeapYear plugin`](./Plugin.md#isleapyear) instead** + `Dayjs` 값이 윤년인지를 확인합니다. 반환 타입은 `boolean` 입니다. ```js diff --git a/docs/pt-br/API-reference.md b/docs/pt-br/API-reference.md index 8859c5f68..948a17e15 100644 --- a/docs/pt-br/API-reference.md +++ b/docs/pt-br/API-reference.md @@ -428,6 +428,8 @@ dayjs().isAfter(dayjs()); // false #### Ano Bissexto +**[DEPRECATED] in 1.7.0, use [`IsLeapYear plugin`](./Plugin.md#isleapyear) instead** + * retorna um Boolean Se um ano é bissexto. diff --git a/docs/zh-cn/API-reference.md b/docs/zh-cn/API-reference.md index 698845158..9462a4de7 100644 --- a/docs/zh-cn/API-reference.md +++ b/docs/zh-cn/API-reference.md @@ -326,6 +326,9 @@ dayjs().isAfter(Dayjs); dayjs().isAfter(dayjs()); // false ``` #### 是否闰年 + +**[已废弃] 将在 1.7.0 废弃, 使用 [`IsLeapYear 插件`](./Plugin.md#isleapyear) 代替** + - return Boolean 是否闰年。