From 9880fe703706205e509d62c4d31ffc1b63c237e3 Mon Sep 17 00:00:00 2001 From: iamkun Date: Sat, 2 Feb 2019 11:20:03 +0800 Subject: [PATCH 1/7] chore(module): add ES2015 module support (#451) --- .npmignore | 1 - package.json | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/.npmignore b/.npmignore index 4f08223c0..3e276781d 100644 --- a/.npmignore +++ b/.npmignore @@ -13,7 +13,6 @@ package-lock.json coverage # dev -src test build .babelrc diff --git a/package.json b/package.json index 1a64995be..05a9cf2cd 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,7 @@ "description": "2KB immutable date time library alternative to Moment.js with the same modern API ", "main": "dayjs.min.js", "types": "index.d.ts", + "module": "./src/index.js", "scripts": { "test": "jest", "test-dst": "TZ=Pacific/Auckland npm run dst-test && TZ=Europe/London npm run dst-test && npm run dst-test", From c4ff46fbc14449bdad9f086d3c9b6839f203961a Mon Sep 17 00:00:00 2001 From: waiting Date: Sat, 2 Feb 2019 11:29:46 +0800 Subject: [PATCH 2/7] chore(typings): default export issue#277 (#278) ref: https://github.com/iamkun/dayjs/issues/277 --- index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.d.ts b/index.d.ts index 1ef70ca57..40f877f5c 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,6 +1,6 @@ -export = dayjs; declare function dayjs (config?: dayjs.ConfigType, option?: dayjs.OptionType): dayjs.Dayjs +export default dayjs declare namespace dayjs { export type ConfigType = string | number | Date | Dayjs From 7287cdb6bc16da1f883ff8dadc30d249866787d5 Mon Sep 17 00:00:00 2001 From: Ferdinand Prantl Date: Sat, 2 Feb 2019 05:10:28 +0100 Subject: [PATCH 3/7] chore: Add LocalizableFormat plugin supplying format options known from Moment.js (#305) --- docs/en/API-reference.md | 1 + docs/en/I18n.md | 8 ++++ docs/en/Plugin.md | 22 ++++++++++ src/locale/bg.js | 10 ++++- src/locale/ca.js | 8 ++++ src/locale/cs.js | 35 ++++++++++++++++ src/locale/da.js | 8 ++++ src/locale/de.js | 8 ++++ src/locale/es.js | 8 ++++ src/locale/fa.js | 8 ++++ src/locale/fr.js | 8 ++++ src/locale/hr.js | 8 ++++ src/locale/id.js | 8 ++++ src/locale/it.js | 8 ++++ src/locale/ja.js | 8 ++++ src/locale/ka.js | 8 ++++ src/locale/ko.js | 8 ++++ src/locale/nb.js | 8 ++++ src/locale/nl.js | 8 ++++ src/locale/pt-br.js | 8 ++++ src/locale/ro.js | 8 ++++ src/locale/ru.js | 8 ++++ src/locale/sv.js | 8 ++++ src/locale/th.js | 8 ++++ src/locale/tr.js | 8 ++++ src/locale/zh-cn.js | 8 ++++ src/locale/zh-tw.js | 8 ++++ src/plugin/localizableFormat/index.js | 24 +++++++++++ test/locale/keys.test.js | 4 ++ test/plugin/localizableFormat.test.js | 60 +++++++++++++++++++++++++++ 30 files changed, 339 insertions(+), 1 deletion(-) create mode 100644 src/locale/cs.js create mode 100644 src/plugin/localizableFormat/index.js create mode 100644 test/plugin/localizableFormat.test.js diff --git a/docs/en/API-reference.md b/docs/en/API-reference.md index d8af8a5b6..171a916de 100644 --- a/docs/en/API-reference.md +++ b/docs/en/API-reference.md @@ -295,6 +295,7 @@ dayjs('2019-01-25').format('DD/MM/YYYY'); // '25/01/2019' | `a` | am pm | | * More available formats `Q Do k kk X x ...` in plugin [`AdvancedFormat`](./Plugin.md#advancedformat) +* Localizable format options compatioble with Moment.js `L LT LTS ...` in plugin [`LocalizableFormat`](./Plugin.md#localizableFormat) ### Difference `.diff(compared: Dayjs, unit: string (default: 'milliseconds'), float?: boolean)` diff --git a/docs/en/I18n.md b/docs/en/I18n.md index c08a1a1b9..417ebe0b1 100644 --- a/docs/en/I18n.md +++ b/docs/en/I18n.md @@ -83,6 +83,14 @@ const localeObject = { 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 + formats: { // abbreviated format options allowing localization + LTS: 'h:mm:ss A', + LT: 'h:mm A', + L: 'MM/DD/YYYY', + LL: 'MMMM D, YYYY', + LLL: 'MMMM D, YYYY h:mm A', + LLLL: 'dddd, MMMM D, YYYY h:mm A' + }, 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', diff --git a/docs/en/Plugin.md b/docs/en/Plugin.md index 75f0c7af3..7c1d535b2 100644 --- a/docs/en/Plugin.md +++ b/docs/en/Plugin.md @@ -65,6 +65,28 @@ List of added formats: | `X` | 1360013296 | Unix Timestamp in second | | `x` | 1360013296123 | Unix Timestamp in millisecond | +### LocalizableFormat + - LocalizableFormat extends `dayjs().format` API to supply localizable format options known from Moment.js. + +```javascript +import localizableFormat from 'dayjs/plugin/localizableFormat' + +dayjs.extend(localizableFormat) + +dayjs().format('L LT') +``` + +List of added formats: + +| Format | English Locale | Sample Output | +| ------ | ------------------------- | --------------------------------- | +| `LT` | h:mm A | 8:02 PM | +| `LTS` | h:mm:ss A | 8:02:18 PM | +| `L` | MM/DD/YYYY | 08/16/2018 | +| `LL` | MMMM D, YYYY | August 16, 2018 | +| `LLL` | MMMM D, YYYY h:mm A | August 16, 2018 8:02 PM | +| `LLLL` | dddd, MMMM D, YYYY h:mm A | Thursday, August 16, 2018 8:02 PM | + ### RelativeTime - RelativeTime adds `.from` `.to` `.fromNow` `.toNow` APIs to formats date to relative time strings (e.g. 3 hours ago). diff --git a/src/locale/bg.js b/src/locale/bg.js index 458dcb3d2..ae97a58a3 100644 --- a/src/locale/bg.js +++ b/src/locale/bg.js @@ -4,7 +4,15 @@ const locale = { name: 'bg', weekdays: 'Неделя_Понеделник_Вторник_Сряда_Четвъртък_Петък_Събота'.split('_'), months: 'Януари_Февруари_Март_Април_Май_Юни_Юли_Август_Септември_Октомври_Ноември_Декември'.split('_'), - ordinal: n => `${n}.` + ordinal: n => `${n}.`, + formats: { + LT: 'H:mm', + LTS: 'H:mm:ss', + L: 'D.MM.YYYY', + LL: 'D MMMM YYYY', + LLL: 'D MMMM YYYY H:mm', + LLLL: 'dddd, D MMMM YYYY H:mm' + } } dayjs.locale(locale, null, true) diff --git a/src/locale/ca.js b/src/locale/ca.js index 83f442c88..017c1c234 100644 --- a/src/locale/ca.js +++ b/src/locale/ca.js @@ -4,6 +4,14 @@ const locale = { name: 'ca', weekdays: 'Diumenge_Dilluns_Dimarts_Dimecres_Dijous_Divendres_Dissabte'.split('_'), months: 'Gener_Febrer_Març_Abril_Maig_Juny_Juliol_Agost_Setembre_Octubre_Novembre_Desembre'.split('_'), + formats: { + LT: 'H:mm', + LTS: 'H:mm:ss', + L: 'DD/MM/YYYY', + LL: 'D MMMM [de] YYYY', + LLL: 'D MMMM [de] YYYY [a les] H:mm', + LLLL: 'dddd D MMMM [de] YYYY [a les] H:mm' + }, relativeTime: { future: 'en %s', past: 'fa %s', diff --git a/src/locale/cs.js b/src/locale/cs.js new file mode 100644 index 000000000..66ade81ef --- /dev/null +++ b/src/locale/cs.js @@ -0,0 +1,35 @@ +import dayjs from 'dayjs' + +const locale = { + name: 'cs', + weekdays: 'neděle_pondělí_úterý_středa_čtvrtek_pátek_sobota'.split('_'), + months: 'leden_únor_březen_duben_květen_červen_červenec_srpen_září_říjen_listopad_prosinec'.split('_'), + ordinal: n => `${n}.`, + formats: { + LT: 'H:mm', + LTS: 'H:mm:ss', + L: 'DD.MM.YYYY', + LL: 'D. MMMM YYYY', + LLL: 'D. MMMM YYYY H:mm', + LLLL: 'dddd D. MMMM YYYY H:mm' + }, + relativeTime: { + future: 'za %s', + past: 'před %s', + s: 'několik sekund', + m: 'minutu', + mm: '%d minut', + h: 'hodinu', + hh: '%d hodin', + d: 'den', + dd: '%d dní', + M: 'měsíc', + MM: '%d měsícú', + y: 'rok', + yy: '%d let' + } +} + +dayjs.locale(locale, null, true) + +export default locale diff --git a/src/locale/da.js b/src/locale/da.js index 6c5a0912b..17b99bce1 100644 --- a/src/locale/da.js +++ b/src/locale/da.js @@ -5,6 +5,14 @@ const locale = { weekdays: 'søndag_mandag_tirsdag_onsdag_torsdag_fredag_lørdag'.split('_'), months: 'januar_februar_marts_april_maj_juni_juli_august_september_oktober_november_december'.split('_'), ordinal: n => `${n}.`, + formats: { + LT: 'HH:mm', + LTS: 'HH:mm:ss', + L: 'DD.MM.YYYY', + LL: 'D. MMMM YYYY', + LLL: 'D. MMMM YYYY HH:mm', + LLLL: 'dddd [d.] D. MMMM YYYY [kl.] HH:mm' + }, relativeTime: { future: 'om %s', past: '%s siden', diff --git a/src/locale/de.js b/src/locale/de.js index a5b5901d0..204b0d6ee 100644 --- a/src/locale/de.js +++ b/src/locale/de.js @@ -6,6 +6,14 @@ const locale = { months: 'Januar_Februar_März_April_Mai_Juni_Juli_August_September_Oktober_November_Dezember'.split('_'), monthsShort: 'Jan_Feb_März_Apr_Mai_Juni_Juli_Aug_Sept_Okt_Nov_Dez'.split('_'), ordinal: n => `${n}.`, + formats: { + LTS: 'HH:mm:ss', + LT: 'HH:mm', + L: 'DD.MM.YYYY', + LL: 'D. MMMM YYYY', + LLL: 'D. MMMM YYYY HH:mm', + LLLL: 'dddd, D. MMMM YYYY HH:mm' + }, relativeTime: { future: 'in %s', past: 'vor %s', diff --git a/src/locale/es.js b/src/locale/es.js index d3c38c6ea..d62accccd 100644 --- a/src/locale/es.js +++ b/src/locale/es.js @@ -5,6 +5,14 @@ const locale = { weekdays: 'Domingo_Lunes_Martes_Miércoles_Jueves_Viernes_Sábado'.split('_'), months: 'Enero_Febrero_Marzo_Abril_Mayo_Junio_Julio_Agosto_Septiembre_Octubre_Noviembre_Diciembre'.split('_'), monthsShort: 'enero_feb_marzo_abr_mayo_jun_jul_agosto_sept_oct_nov_dic'.split('_'), + formats: { + LT: 'H:mm', + LTS: 'H:mm:ss', + L: 'DD/MM/YYYY', + LL: 'D [de] MMMM [de] YYYY', + LLL: 'D [de] MMMM [de] YYYY H:mm', + LLLL: 'dddd, D [de] MMMM [de] YYYY H:mm' + }, relativeTime: { future: 'en %s', past: 'hace %s', diff --git a/src/locale/fa.js b/src/locale/fa.js index c2bd11108..e7b64ea4c 100644 --- a/src/locale/fa.js +++ b/src/locale/fa.js @@ -5,6 +5,14 @@ const locale = { weekdays: 'یک‌شنبه_دوشنبه_سه‌شنبه_چهارشنبه_پنج‌شنبه_جمعه_شنبه'.split('_'), months: 'ژانویه_فوریه_مارس_آوریل_مه_ژوئن_ژوئیه_اوت_سپتامبر_اکتبر_نوامبر_دسامبر'.split('_'), ordinal: n => n, + formats: { + LT: 'HH:mm', + LTS: 'HH:mm:ss', + L: 'DD/MM/YYYY', + LL: 'D MMMM YYYY', + LLL: 'D MMMM YYYY HH:mm', + LLLL: 'dddd, D MMMM YYYY HH:mm' + }, relativeTime: { future: '%s در', past: 'پیش %s', diff --git a/src/locale/fr.js b/src/locale/fr.js index ea9a11002..9ad79a4a3 100644 --- a/src/locale/fr.js +++ b/src/locale/fr.js @@ -5,6 +5,14 @@ const locale = { weekdays: 'Dimanche_Lundi_Mardi_Mercredi_Jeudi_Vendredi_Samedi'.split('_'), months: 'Janvier_Février_Mars_Avril_Mai_Juin_Juillet_Août_Septembre_Octobre_Novembre_Décembre'.split('_'), monthsShort: 'janv_févr_mars_avril_mai_juin_juil_août_sept_oct_nov_déc'.split('_'), + formats: { + LT: 'HH:mm', + LTS: 'HH:mm:ss', + L: 'DD/MM/YYYY', + LL: 'D MMMM YYYY', + LLL: 'D MMMM YYYY HH:mm', + LLLL: 'dddd D MMMM YYYY HH:mm' + }, relativeTime: { future: 'dans %s', past: 'il y a %s', diff --git a/src/locale/hr.js b/src/locale/hr.js index 7719b628f..e19fbb567 100644 --- a/src/locale/hr.js +++ b/src/locale/hr.js @@ -4,6 +4,14 @@ const locale = { name: 'hr', weekdays: 'Nedjelja_Ponedjeljak_Utorak_Srijeda_Četvrtak_Petak_Subota'.split('_'), months: 'Siječanj_Veljača_Ožujak_Travanj_Svibanj_Lipanj_Srpanj_Kolovoz_Rujan_Listopad_Studeni_Prosinac'.split('_'), + formats: { + LT: 'H:mm', + LTS: 'H:mm:ss', + L: 'DD.MM.YYYY', + LL: 'D. MMMM YYYY', + LLL: 'D. MMMM YYYY H:mm', + LLLL: 'dddd, D. MMMM YYYY H:mm' + }, relativeTime: { future: 'za %s', past: 'prije %s', diff --git a/src/locale/id.js b/src/locale/id.js index 30d4cd7c7..fa033abf8 100644 --- a/src/locale/id.js +++ b/src/locale/id.js @@ -4,6 +4,14 @@ const locale = { name: 'id', weekdays: 'Minggu_Senin_Selasa_Rabu_Kamis_Jumat_Sabtu'.split('_'), months: 'Januari_Februari_Maret_April_Mei_Juni_Juli_Agustus_September_Oktober_November_Desember'.split('_'), + formats: { + LT: 'HH.mm', + LTS: 'HH.mm.ss', + L: 'DD/MM/YYYY', + LL: 'D MMMM YYYY', + LLL: 'D MMMM YYYY [pukul] HH.mm', + LLLL: 'dddd, D MMMM YYYY [pukul] HH.mm' + }, relativeTime: { future: 'dalam %s', past: '%s yang lalu', diff --git a/src/locale/it.js b/src/locale/it.js index c57f5af69..acd0b6202 100644 --- a/src/locale/it.js +++ b/src/locale/it.js @@ -5,6 +5,14 @@ const locale = { weekdays: 'Domenica_Lunedì_Martedì_Mercoledì_Giovedì_Venerdì_Sabato'.split('_'), months: 'Gennaio_Febbraio_Marzo_Aprile_Maggio_Giugno_Luglio_Agosto_Settembre_Ottobre_Novembre_Dicembre'.split('_'), monthsShort: 'genn_febbr_mar_apr_magg_giugno_luglio_ag_sett_ott_nov_dic'.split('_'), + formats: { + LT: 'HH:mm', + LTS: 'HH:mm:ss', + L: 'DD/MM/YYYY', + LL: 'D MMMM YYYY', + LLL: 'D MMMM YYYY HH:mm', + LLLL: 'dddd D MMMM YYYY HH:mm' + }, relativeTime: { future: 'tra %s', past: 'da %s', diff --git a/src/locale/ja.js b/src/locale/ja.js index 9800fa9e8..a088ff666 100644 --- a/src/locale/ja.js +++ b/src/locale/ja.js @@ -7,6 +7,14 @@ const locale = { weekdaysMin: '日_月_火_水_木_金_土'.split('_'), months: '1月_2月_3月_4月_5月_6月_7月_8月_9月_10月_11月_12月'.split('_'), ordinal: n => `${n}日`, + formats: { + LT: 'HH:mm', + LTS: 'HH:mm:ss', + L: 'YYYY/MM/DD', + LL: 'YYYY年M月D日', + LLL: 'YYYY年M月D日 HH:mm', + LLLL: 'YYYY年M月D日 dddd HH:mm' + }, relativeTime: { future: '%s後', past: '%s前', diff --git a/src/locale/ka.js b/src/locale/ka.js index 1ceece54a..43d06b0b4 100644 --- a/src/locale/ka.js +++ b/src/locale/ka.js @@ -4,6 +4,14 @@ const locale = { name: 'ka', weekdays: 'კვირა_ორშაბათი_სამშაბათი_გარემო_ხუთშაბათი_პარასკევი_შაბათს'.split('_'), months: 'იანვარი_თებერვალი_მარტი_აპრილი_მაისი_ივნისი_ივლისი_აგვისტო_სექტემბერი_ოქტომბერი_ნოემბერი_დეკემბერი'.split('_'), + formats: { + LT: 'h:mm A', + LTS: 'h:mm:ss A', + L: 'DD/MM/YYYY', + LL: 'D MMMM YYYY', + LLL: 'D MMMM YYYY h:mm A', + LLLL: 'dddd, D MMMM YYYY h:mm A' + }, relativeTime: { future: '%s შემდეგ', past: '%s წინ', diff --git a/src/locale/ko.js b/src/locale/ko.js index 02b75d4a7..c5d3860a9 100644 --- a/src/locale/ko.js +++ b/src/locale/ko.js @@ -5,6 +5,14 @@ const locale = { weekdays: '일요일_월요일_화요일_수요일_목요일_금요일_토요일'.split('_'), months: '1월_2월_3월_4월_5월_6월_7월_8월_9월_10월_11월_12월'.split('_'), ordinal: n => n, + formats: { + LT: 'A h:mm', + LTS: 'A h:mm:ss', + L: 'YYYY.MM.DD.', + LL: 'YYYY년 MMMM D일', + LLL: 'YYYY년 MMMM D일 A h:mm', + LLLL: 'YYYY년 MMMM D일 dddd A h:mm' + }, relativeTime: { future: '%s 후', past: '%s 전', diff --git a/src/locale/nb.js b/src/locale/nb.js index 9cf2591d8..41288a1e3 100644 --- a/src/locale/nb.js +++ b/src/locale/nb.js @@ -5,6 +5,14 @@ const locale = { weekdays: 'søndag_mandag_tirsdag_onsdag_torsdag_fredag_lørdag'.split('_'), months: 'januar_februar_mars_april_mai_juni_juli_august_september_oktober_november_desember'.split('_'), ordinal: n => `${n}.`, + formats: { + LT: 'HH:mm', + LTS: 'HH:mm:ss', + L: 'DD.MM.YYYY', + LL: 'D. MMMM YYYY', + LLL: 'D. MMMM YYYY [kl.] HH:mm', + LLLL: 'dddd D. MMMM YYYY [kl.] HH:mm' + }, relativeTime: { future: 'om %s', past: 'for %s siden', diff --git a/src/locale/nl.js b/src/locale/nl.js index bd71b14ec..a8cacd1c0 100644 --- a/src/locale/nl.js +++ b/src/locale/nl.js @@ -5,6 +5,14 @@ const locale = { weekdays: 'zondag_maandag_dinsdag_woensdag_donderdag_vrijdag_zaterdag'.split('_'), months: 'januari_februari_maart_april_mei_juni_juli_augustus_september_oktober_november_december'.split('_'), ordinal: n => `${n}.`, + formats: { + LT: 'HH:mm', + LTS: 'HH:mm:ss', + L: 'DD-MM-YYYY', + LL: 'D MMMM YYYY', + LLL: 'D MMMM YYYY HH:mm', + LLLL: 'dddd D MMMM YYYY HH:mm' + }, relativeTime: { future: 'over %s', past: '%s geleden', diff --git a/src/locale/pt-br.js b/src/locale/pt-br.js index 12f57fe37..3ac0c250e 100644 --- a/src/locale/pt-br.js +++ b/src/locale/pt-br.js @@ -5,6 +5,14 @@ const locale = { weekdays: 'Domingo_Segunda-feira_Terça-feira_Quarta-feira_Quinta-feira_Sexta-feira_Sábado'.split('_'), months: 'Janeiro_Fevereiro_Março_Abril_Maio_Junho_Julho_Agosto_Setembro_Outubro_Novembro_Dezembro'.split('_'), ordinal: n => `${n}º`, + formats: { + LT: 'HH:mm', + LTS: 'HH:mm:ss', + L: 'DD/MM/YYYY', + LL: 'D [de] MMMM [de] YYYY', + LLL: 'D [de] MMMM [de] YYYY [às] HH:mm', + LLLL: 'dddd, D [de] MMMM [de] YYYY [às] HH:mm' + }, relativeTime: { future: 'em %s', past: 'há %s', diff --git a/src/locale/ro.js b/src/locale/ro.js index c2b15317c..f792add31 100644 --- a/src/locale/ro.js +++ b/src/locale/ro.js @@ -4,6 +4,14 @@ const locale = { name: 'ro', weekdays: 'Duminică_Luni_Marți_Miercuri_Joi_Vineri_Sâmbătă'.split('_'), months: 'Ianuarie_Februarie_Martie_Aprilie_Mai_Iunie_Iulie_August_Septembrie_Octombrie_Noiembrie_Decembrie'.split('_'), + formats: { + LT: 'H:mm', + LTS: 'H:mm:ss', + L: 'DD.MM.YYYY', + LL: 'D MMMM YYYY', + LLL: 'D MMMM YYYY H:mm', + LLLL: 'dddd, D MMMM YYYY H:mm' + }, relativeTime: { future: 'peste %s', past: 'acum %s', diff --git a/src/locale/ru.js b/src/locale/ru.js index 38c125405..d59d0cdbb 100644 --- a/src/locale/ru.js +++ b/src/locale/ru.js @@ -7,6 +7,14 @@ const locale = { weekdaysMin: 'вс_пн_вт_ср_чт_пт_сб'.split('_'), months: 'январь_февраль_март_апрель_май_июнь_июль_август_сентябрь_октябрь_ноябрь_декабрь'.split('_'), monthsShort: 'янв_фев_мар_апр_май_июн_июл_авг_сен_окт_ноя_дек'.split('_'), + formats: { + LT: 'H:mm', + LTS: 'H:mm:ss', + L: 'DD.MM.YYYY', + LL: 'D MMMM YYYY г.', + LLL: 'D MMMM YYYY г., H:mm', + LLLL: 'dddd, D MMMM YYYY г., H:mm' + }, relativeTime: { future: 'через %s', past: '%s назад', diff --git a/src/locale/sv.js b/src/locale/sv.js index 970822c2a..e70ae23a3 100644 --- a/src/locale/sv.js +++ b/src/locale/sv.js @@ -9,6 +9,14 @@ const locale = { const o = (b === 1) || (b === 2) ? 'a' : 'e' return `${n}${o}` }, + formats: { + LT: 'HH:mm', + LTS: 'HH:mm:ss', + L: 'YYYY-MM-DD', + LL: 'D MMMM YYYY', + LLL: 'D MMMM YYYY [kl.] HH:mm', + LLLL: 'dddd D MMMM YYYY [kl.] HH:mm' + }, relativeTime: { future: 'om %s', past: 'för %s sedan', diff --git a/src/locale/th.js b/src/locale/th.js index a9cc8fc52..6b2ca4485 100644 --- a/src/locale/th.js +++ b/src/locale/th.js @@ -7,6 +7,14 @@ const locale = { weekdaysMin: 'อา._จ._อ._พ._พฤ._ศ._ส.'.split('_'), months: 'มกราคม_กุมภาพันธ์_มีนาคม_เมษายน_พฤษภาคม_มิถุนายน_กรกฏาคม_สิงหาคม_กันยายน_ตุลาคม_พฤศจิกายน_ธันวาคม'.split('_'), monthsShort: 'ม.ค._ก.พ._มี.ค._เม.ย._พ.ค._มิ.ย._ก.ค._ส.ค._ก.ย._ต.ค._พ.ย._ธ.ค.'.split('_'), + formats: { + LT: 'H:mm', + LTS: 'H:mm:ss', + L: 'DD/MM/YYYY', + LL: 'D MMMM YYYY', + LLL: 'D MMMM YYYY เวลา H:mm', + LLLL: 'วันddddที่ D MMMM YYYY เวลา H:mm' + }, relativeTime: { future: 'อีก %s', past: '%s ที่ผ่านมา', diff --git a/src/locale/tr.js b/src/locale/tr.js index 6843cfa09..1e92eefd7 100644 --- a/src/locale/tr.js +++ b/src/locale/tr.js @@ -4,6 +4,14 @@ const locale = { name: 'tr', weekdays: 'Pazar_Pazartesi_Salı_Çarşamba_Perşembe_Cuma_Cumartesi'.split('_'), months: 'Ocak_Şubat_Mart_Nisan_Mayıs_Haziran_Temmuz_Ağustos_Eylül_Ekim_Kasım_Aralık'.split('_'), + formats: { + LT: 'HH:mm', + LTS: 'HH:mm:ss', + L: 'DD.MM.YYYY', + LL: 'D MMMM YYYY', + LLL: 'D MMMM YYYY HH:mm', + LLLL: 'dddd, D MMMM YYYY HH:mm' + }, relativeTime: { future: '%s sonra', past: '%s önce', diff --git a/src/locale/zh-cn.js b/src/locale/zh-cn.js index 31e0d996b..f30c297e9 100644 --- a/src/locale/zh-cn.js +++ b/src/locale/zh-cn.js @@ -8,6 +8,14 @@ const locale = { months: '一月_二月_三月_四月_五月_六月_七月_八月_九月_十月_十一月_十二月'.split('_'), monthsShort: '1月_2月_3月_4月_5月_6月_7月_8月_9月_10月_11月_12月'.split('_'), ordinal: n => `${n}日`, + formats: { + LT: 'HH:mm', + LTS: 'HH:mm:ss', + L: 'YYYY/MM/DD', + LL: 'YYYY年M月D日', + LLL: 'YYYY年M月D日Ah点mm分', + LLLL: 'YYYY年M月D日ddddAh点mm分' + }, relativeTime: { future: '%s内', past: '%s前', diff --git a/src/locale/zh-tw.js b/src/locale/zh-tw.js index 3886d12a8..91a6e02e7 100644 --- a/src/locale/zh-tw.js +++ b/src/locale/zh-tw.js @@ -8,6 +8,14 @@ const locale = { months: '一月_二月_三月_四月_五月_六月_七月_八月_九月_十月_十一月_十二月'.split('_'), monthsShort: '1月_2月_3月_4月_5月_6月_7月_8月_9月_10月_11月_12月'.split('_'), ordinal: n => `${n}日`, + formats: { + LT: 'HH:mm', + LTS: 'HH:mm:ss', + L: 'YYYY/MM/DD', + LL: 'YYYY年M月D日', + LLL: 'YYYY年M月D日 HH:mm', + LLLL: 'YYYY年M月D日dddd HH:mm' + }, relativeTime: { future: '%s內', past: '%s前', diff --git a/src/plugin/localizableFormat/index.js b/src/plugin/localizableFormat/index.js new file mode 100644 index 000000000..37d4db0ab --- /dev/null +++ b/src/plugin/localizableFormat/index.js @@ -0,0 +1,24 @@ +import { FORMAT_DEFAULT } from '../../constant' + +export default (o, c, d) => { + const proto = c.prototype + const oldFormat = proto.format + const englishFormats = { + LTS: 'h:mm:ss A', + LT: 'h:mm A', + L: 'MM/DD/YYYY', + LL: 'MMMM D, YYYY', + LLL: 'MMMM D, YYYY h:mm A', + LLLL: 'dddd, MMMM D, YYYY h:mm A' + } + d.en.formats = englishFormats + proto.format = function (formatStr) { + const locale = this.$locale() + const formats = locale.formats || {} + const str = formatStr || FORMAT_DEFAULT + const result = str.replace(/LTS|LT|L{1,4}/g, match => + formats[match] || englishFormats[match]) + return oldFormat.call(this, result) + } +} + diff --git a/test/locale/keys.test.js b/test/locale/keys.test.js index 68219c751..85746e524 100644 --- a/test/locale/keys.test.js +++ b/test/locale/keys.test.js @@ -19,6 +19,7 @@ it('Locale keys', () => { ordinal, weekdays, months, + formats, relativeTime, weekdaysShort, monthsShort, @@ -36,6 +37,9 @@ it('Locale keys', () => { expect(ordinal(1)).toEqual(expect.anything()) expect(ordinal(3)).toEqual(expect.anything()) expect(dayjs().locale(name).$locale().name).toBe(name) + if (formats) { + expect(Object.keys(formats).sort()).toEqual(['L', 'LL', 'LLL', 'LLLL', 'LT', 'LTS'].sort()) + } if (relativeTime) { expect(Object.keys(relativeTime).sort()).toEqual(['d', 'dd', 'future', 'h', 'hh', 'm', 'mm', 'M', 'MM', 'past', 's', 'y', 'yy'] diff --git a/test/plugin/localizableFormat.test.js b/test/plugin/localizableFormat.test.js new file mode 100644 index 000000000..e74c3b390 --- /dev/null +++ b/test/plugin/localizableFormat.test.js @@ -0,0 +1,60 @@ +import MockDate from 'mockdate' +import moment from 'moment' +import dayjs from '../../src' +import es from '../../src/locale/es' +import ar from '../../src/locale/ar' +import localizableFormat from '../../src/plugin/localizableFormat' + +dayjs.extend(localizableFormat) + +beforeEach(() => { + MockDate.set(new Date()) +}) + +afterEach(() => { + MockDate.reset() +}) + +it('Declares English localizable formats', () => { + expect(dayjs.en).toBeDefined() + expect(dayjs.en.formats).toBeDefined(); + ['LT', 'LTS', 'L', 'LL', 'LLL', 'LLLL'].forEach(option => + expect(dayjs.en.formats[option]).toBeDefined()) +}) + +it('Recognizes localizable format options', () => { + const { formats } = dayjs.en + const date = dayjs(); + ['LT', 'LTS', 'L', 'LL', 'LLL', 'LLLL'].forEach(option => + expect(date.format(option)).toBe(date.format(formats[option]))) +}) + +it('Uses correct English formats', () => { + const date = new Date() + const actualDate = dayjs(date) + const expectedDate = moment(date); + ['LT', 'LTS', 'L', 'LL', 'LLL', 'LLLL'].forEach(option => + expect(actualDate.format(option)).toBe(expectedDate.format(option))) +}) + +it('Uses English formats in other locales as default', () => { + const date = new Date() + const actualDate = dayjs(date) + const expectedDate = moment(date) + // todo: ar here isn't a good fix here + expect(actualDate.locale(ar).format('L')).toBe(expectedDate.format('L')) +}) + +it('Leaves the default format intact', () => { + const date = new Date() + const actualDate = dayjs(date) + const expectedDate = moment(date) + expect(actualDate.format()).toBe(expectedDate.format()) +}) + +it('Uses the locale of the dayjs instance', () => { + const date = new Date() + const englishDate = dayjs(date) + const spanishDate = dayjs(date, { locale: es }) + expect(englishDate.format('L LTS')).not.toBe(spanishDate.format('L LTS')) +}) From 1e185b5d364380f541abc67d9a4d189cff988097 Mon Sep 17 00:00:00 2001 From: iamkun Date: Sat, 2 Feb 2019 13:25:36 +0800 Subject: [PATCH 4/7] chore: rename plugin LocalizableFormat to LocalizedFormat (#471) --- docs/en/API-reference.md | 2 +- docs/en/Plugin.md | 8 ++++---- .../{localizableFormat => localizedFormat}/index.js | 0 test/plugin/localizableFormat.test.js | 8 ++++---- 4 files changed, 9 insertions(+), 9 deletions(-) rename src/plugin/{localizableFormat => localizedFormat}/index.js (100%) diff --git a/docs/en/API-reference.md b/docs/en/API-reference.md index 171a916de..e95153871 100644 --- a/docs/en/API-reference.md +++ b/docs/en/API-reference.md @@ -295,7 +295,7 @@ dayjs('2019-01-25').format('DD/MM/YYYY'); // '25/01/2019' | `a` | am pm | | * More available formats `Q Do k kk X x ...` in plugin [`AdvancedFormat`](./Plugin.md#advancedformat) -* Localizable format options compatioble with Moment.js `L LT LTS ...` in plugin [`LocalizableFormat`](./Plugin.md#localizableFormat) +* Localized format options `L LT LTS ...` in plugin [`LocalizedFormat`](./Plugin.md#localizedFormat) ### Difference `.diff(compared: Dayjs, unit: string (default: 'milliseconds'), float?: boolean)` diff --git a/docs/en/Plugin.md b/docs/en/Plugin.md index 7c1d535b2..ec35369e5 100644 --- a/docs/en/Plugin.md +++ b/docs/en/Plugin.md @@ -65,13 +65,13 @@ List of added formats: | `X` | 1360013296 | Unix Timestamp in second | | `x` | 1360013296123 | Unix Timestamp in millisecond | -### LocalizableFormat - - LocalizableFormat extends `dayjs().format` API to supply localizable format options known from Moment.js. +### LocalizedFormat + - LocalizedFormat extends `dayjs().format` API to supply localized format options known from Moment.js. ```javascript -import localizableFormat from 'dayjs/plugin/localizableFormat' +import LocalizedFormat from 'dayjs/plugin/localizedFormat' -dayjs.extend(localizableFormat) +dayjs.extend(LocalizedFormat) dayjs().format('L LT') ``` diff --git a/src/plugin/localizableFormat/index.js b/src/plugin/localizedFormat/index.js similarity index 100% rename from src/plugin/localizableFormat/index.js rename to src/plugin/localizedFormat/index.js diff --git a/test/plugin/localizableFormat.test.js b/test/plugin/localizableFormat.test.js index e74c3b390..5e6b3055d 100644 --- a/test/plugin/localizableFormat.test.js +++ b/test/plugin/localizableFormat.test.js @@ -3,9 +3,9 @@ import moment from 'moment' import dayjs from '../../src' import es from '../../src/locale/es' import ar from '../../src/locale/ar' -import localizableFormat from '../../src/plugin/localizableFormat' +import localizedFormat from '../../src/plugin/localizedFormat' -dayjs.extend(localizableFormat) +dayjs.extend(localizedFormat) beforeEach(() => { MockDate.set(new Date()) @@ -15,14 +15,14 @@ afterEach(() => { MockDate.reset() }) -it('Declares English localizable formats', () => { +it('Declares English localized formats', () => { expect(dayjs.en).toBeDefined() expect(dayjs.en.formats).toBeDefined(); ['LT', 'LTS', 'L', 'LL', 'LLL', 'LLLL'].forEach(option => expect(dayjs.en.formats[option]).toBeDefined()) }) -it('Recognizes localizable format options', () => { +it('Recognizes localized format options', () => { const { formats } = dayjs.en const date = dayjs(); ['LT', 'LTS', 'L', 'LL', 'LLL', 'LLLL'].forEach(option => From 7e040040962cea1019f57ad7c5b67380275efd18 Mon Sep 17 00:00:00 2001 From: Dennis Fridrich Date: Tue, 22 Jan 2019 09:58:02 +0100 Subject: [PATCH 5/7] fix(): Add missing czech language locale (#461) * Add Czech support --- src/locale/cs.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/locale/cs.js b/src/locale/cs.js index 66ade81ef..1f4393f41 100644 --- a/src/locale/cs.js +++ b/src/locale/cs.js @@ -17,16 +17,16 @@ const locale = { future: 'za %s', past: 'před %s', s: 'několik sekund', - m: 'minutu', + m: 'minuta', mm: '%d minut', - h: 'hodinu', + h: 'hodina', hh: '%d hodin', d: 'den', - dd: '%d dní', + dd: '%d dnů', M: 'měsíc', - MM: '%d měsícú', + MM: '%d měsíců', y: 'rok', - yy: '%d let' + yy: '%d roků' } } From f6d9a6441a8b5a5815e690914bed4422289e2024 Mon Sep 17 00:00:00 2001 From: iamkun Date: Sat, 2 Feb 2019 12:14:56 +0800 Subject: [PATCH 6/7] fix: fix it locale error (#458) * fix: fix it locale error * fix: fix es locale monthsShort error * test: fix test * fix: add missing es-do and es-us locale file --- src/locale/{es-es.js => es-do.js} | 7 +++++-- src/locale/es-us.js | 30 ++++++++++++++++++++++++++++++ src/locale/es.js | 6 ++++-- src/locale/it.js | 8 +++++--- test/locale.test.js | 26 +++++++++++++------------- 5 files changed, 57 insertions(+), 20 deletions(-) rename src/locale/{es-es.js => es-do.js} (66%) create mode 100644 src/locale/es-us.js diff --git a/src/locale/es-es.js b/src/locale/es-do.js similarity index 66% rename from src/locale/es-es.js rename to src/locale/es-do.js index ee630fdaa..434584ecd 100644 --- a/src/locale/es-es.js +++ b/src/locale/es-do.js @@ -1,9 +1,12 @@ import dayjs from 'dayjs' const locale = { - name: 'es-es', - weekdays: 'Domingo_Lunes_Martes_Miércoles_Jueves_Viernes_Sábado'.split('_'), + name: 'es-do', + weekdays: 'domingo_lunes_martes_miércoles_jueves_viernes_sábado'.split('_'), + weekdaysShort: 'dom._lun._mar._mié._jue._vie._sáb.'.split('_'), + weekdaysMin: 'do_lu_ma_mi_ju_vi_sá'.split('_'), months: 'Enero_Febrero_Marzo_Abril_Mayo_Junio_Julio_Agosto_Septiembre_Octubre_Noviembre_Diciembre'.split('_'), + monthsShort: 'ene_feb_mar_abr_may_jun_jul_ago_sep_oct_nov_dic'.split('_'), relativeTime: { future: 'en %s', past: 'hace %s', diff --git a/src/locale/es-us.js b/src/locale/es-us.js new file mode 100644 index 000000000..ba5b74d9a --- /dev/null +++ b/src/locale/es-us.js @@ -0,0 +1,30 @@ +import dayjs from 'dayjs' + +const locale = { + name: 'es-us', + weekdays: 'domingo_lunes_martes_miércoles_jueves_viernes_sábado'.split('_'), + weekdaysShort: 'dom._lun._mar._mié._jue._vie._sáb.'.split('_'), + weekdaysMin: 'do_lu_ma_mi_ju_vi_sá'.split('_'), + months: 'Enero_Febrero_Marzo_Abril_Mayo_Junio_Julio_Agosto_Septiembre_Octubre_Noviembre_Diciembre'.split('_'), + monthsShort: 'ene_feb_mar_abr_may_jun_jul_ago_sep_oct_nov_dic'.split('_'), + relativeTime: { + future: 'en %s', + past: 'hace %s', + s: 'unos segundos', + m: 'un minuto', + mm: '%d minutos', + h: 'una hora', + hh: '%d horas', + d: 'un día', + dd: '%d días', + M: 'un mes', + MM: '%d meses', + y: 'un año', + yy: '%d años' + }, + ordinal: n => `${n}º` +} + +dayjs.locale(locale, null, true) + +export default locale diff --git a/src/locale/es.js b/src/locale/es.js index d62accccd..e39e4b175 100644 --- a/src/locale/es.js +++ b/src/locale/es.js @@ -2,9 +2,11 @@ import dayjs from 'dayjs' const locale = { name: 'es', - weekdays: 'Domingo_Lunes_Martes_Miércoles_Jueves_Viernes_Sábado'.split('_'), + monthsShort: 'ene_feb_mar_abr_may_jun_jul_ago_sep_oct_nov_dic'.split('_'), + weekdays: 'domingo_lunes_martes_miércoles_jueves_viernes_sábado'.split('_'), + weekdaysShort: 'dom._lun._mar._mié._jue._vie._sáb.'.split('_'), + weekdaysMin: 'do_lu_ma_mi_ju_vi_sá'.split('_'), months: 'Enero_Febrero_Marzo_Abril_Mayo_Junio_Julio_Agosto_Septiembre_Octubre_Noviembre_Diciembre'.split('_'), - monthsShort: 'enero_feb_marzo_abr_mayo_jun_jul_agosto_sept_oct_nov_dic'.split('_'), formats: { LT: 'H:mm', LTS: 'H:mm:ss', diff --git a/src/locale/it.js b/src/locale/it.js index acd0b6202..f5cd43c46 100644 --- a/src/locale/it.js +++ b/src/locale/it.js @@ -2,9 +2,11 @@ import dayjs from 'dayjs' const locale = { name: 'it', - weekdays: 'Domenica_Lunedì_Martedì_Mercoledì_Giovedì_Venerdì_Sabato'.split('_'), - months: 'Gennaio_Febbraio_Marzo_Aprile_Maggio_Giugno_Luglio_Agosto_Settembre_Ottobre_Novembre_Dicembre'.split('_'), - monthsShort: 'genn_febbr_mar_apr_magg_giugno_luglio_ag_sett_ott_nov_dic'.split('_'), + weekdays: 'domenica_lunedì_martedì_mercoledì_giovedì_venerdì_sabato'.split('_'), + weekdaysShort: 'dom_lun_mar_mer_gio_ven_sab'.split('_'), + weekdaysMin: 'do_lu_ma_me_gi_ve_sa'.split('_'), + months: 'gennaio_febbraio_marzo_aprile_maggio_giugno_luglio_agosto_settembre_ottobre_novembre_dicembre'.split('_'), + monthsShort: 'gen_feb_mar_apr_mag_giu_lug_ago_set_ott_nov_dic'.split('_'), formats: { LT: 'HH:mm', LTS: 'HH:mm:ss', diff --git a/test/locale.test.js b/test/locale.test.js index c1daa8dad..06560b6ba 100644 --- a/test/locale.test.js +++ b/test/locale.test.js @@ -15,7 +15,7 @@ const format = 'dddd D, MMMM' it('Uses spanish locale through constructor', () => { // not recommend expect(dayjs('2018-4-28', { locale: es }) .format(format)) - .toBe('Sábado 28, Abril') + .toBe('sábado 28, Abril') }) it('set locale for one instance only', () => { @@ -25,7 +25,7 @@ it('set locale for one instance only', () => { expect(dayjs('2018-4-28') .locale(es).format(format)) - .toBe('Sábado 28, Abril') + .toBe('sábado 28, Abril') expect(dayjs('2018-4-28') .format(format)) @@ -38,7 +38,7 @@ it('set global locale', () => { .toBe('Saturday 28, April') dayjs.locale(es) expect(dayjs('2018-4-28').format(format)) - .toBe('Sábado 28, Abril') + .toBe('sábado 28, Abril') dayjs.locale('en') expect(dayjs('2018-4-28').format(format)) .toBe('Saturday 28, April') @@ -50,10 +50,10 @@ it('immutable instance locale', () => { expect(origin.format(format)) .toBe('Saturday 28, April') expect(origin.locale('es').format(format)) - .toBe('Sábado 28, Abril') + .toBe('sábado 28, Abril') const changed = origin.locale('es') expect(changed.format(format)) - .toBe('Sábado 28, Abril') + .toBe('sábado 28, Abril') expect(origin.format(format)) .toBe('Saturday 28, April') }) @@ -73,29 +73,29 @@ describe('Instance locale inheritance', () => { it('Clone', () => { expect(esDayjs.clone().format(format)) - .toBe('Sábado 28, Abril') + .toBe('sábado 28, Abril') expect(dayjs(esDayjs).format(format)) - .toBe('Sábado 28, Abril') + .toBe('sábado 28, Abril') }) it('StartOf EndOf', () => { expect(esDayjs.startOf('year').format(format)) - .toBe('Lunes 1, Enero') + .toBe('lunes 1, Enero') expect(esDayjs.endOf('day').format(format)) - .toBe('Sábado 28, Abril') + .toBe('sábado 28, Abril') }) it('Set', () => { expect(esDayjs.set('year', 2017).format(format)) - .toBe('Viernes 28, Abril') + .toBe('viernes 28, Abril') }) it('Add', () => { expect(esDayjs.add(1, 'year').format(format)) - .toBe('Domingo 28, Abril') + .toBe('domingo 28, Abril') expect(esDayjs.add(1, 'month').format(format)) - .toBe('Lunes 28, Mayo') + .toBe('lunes 28, Mayo') expect(esDayjs.add(1, 'minute').format(format)) - .toBe('Sábado 28, Abril') + .toBe('sábado 28, Abril') }) }) From 3bd06f2a028da36275d65b1aa6fdf340dbd779b3 Mon Sep 17 00:00:00 2001 From: louis <1891109+dotlouis@users.noreply.github.com> Date: Sat, 2 Feb 2019 05:18:21 +0100 Subject: [PATCH 7/7] chore: Added shorthand units to UnitType (#470) --- index.d.ts | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/index.d.ts b/index.d.ts index 40f877f5c..de605a8a8 100644 --- a/index.d.ts +++ b/index.d.ts @@ -7,7 +7,11 @@ declare namespace dayjs { export type OptionType = { locale: string } - export type UnitType = 'millisecond' | 'second' | 'minute' | 'hour' | 'day' | 'week' | 'month' | 'quarter' | 'year' | 'date' + type UnitTypeShort = 'd' | 'M' | 'y' | 'h' | 'm' | 's' | 'ms' + export type UnitType = 'millisecond' | 'second' | 'minute' | 'hour' | 'day' | 'month' | 'quarter' | 'year' | 'date' | UnitTypeShort; + + type OpUnitTypeShort = 'w' + export type OpUnitType = UnitType | "week" | OpUnitTypeShort; interface DayjsObject { years: number @@ -44,17 +48,17 @@ declare namespace dayjs { set(unit: UnitType, value: number): Dayjs - add(value: number, unit: UnitType): Dayjs + add(value: number, unit: OpUnitType): Dayjs - subtract(value: number, unit: UnitType): Dayjs + subtract(value: number, unit: OpUnitType): Dayjs - startOf(unit: UnitType): Dayjs + startOf(unit: OpUnitType): Dayjs - endOf(unit: UnitType): Dayjs + endOf(unit: OpUnitType): Dayjs format(template?: string): string - diff(dayjs: Dayjs, unit: UnitType, float?: boolean): number + diff(dayjs: Dayjs, unit: OpUnitType, float?: boolean): number valueOf(): number @@ -74,11 +78,11 @@ declare namespace dayjs { toString(): string - isBefore(dayjs: Dayjs, unit?: UnitType): boolean + isBefore(dayjs: Dayjs, unit?: OpUnitType): boolean - isSame(dayjs: Dayjs, unit?: UnitType): boolean + isSame(dayjs: Dayjs, unit?: OpUnitType): boolean - isAfter(dayjs: Dayjs, unit?: UnitType): boolean + isAfter(dayjs: Dayjs, unit?: OpUnitType): boolean isLeapYear(): boolean