repo stringlengths 5 106 | file_url stringlengths 78 301 | file_path stringlengths 4 211 | content stringlengths 0 32.8k | language stringclasses 1
value | license stringclasses 7
values | commit_sha stringlengths 40 40 | retrieved_at stringdate 2026-01-04 14:56:49 2026-01-05 02:23:25 | truncated bool 2
classes |
|---|---|---|---|---|---|---|---|---|
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/src/locale/tr.js | src/locale/tr.js | // Turkish [tr]
import dayjs from 'dayjs'
const locale = {
name: 'tr',
weekdays: 'Pazar_Pazartesi_Salı_Çarşamba_Perşembe_Cuma_Cumartesi'.split('_'),
weekdaysShort: 'Paz_Pts_Sal_Çar_Per_Cum_Cts'.split('_'),
weekdaysMin: 'Pz_Pt_Sa_Ça_Pe_Cu_Ct'.split('_'),
months: 'Ocak_Şubat_Mart_Nisan_Mayıs_Haziran_Temmuz_Ağustos_Eylül_Ekim_Kasım_Aralık'.split('_'),
monthsShort: 'Oca_Şub_Mar_Nis_May_Haz_Tem_Ağu_Eyl_Eki_Kas_Ara'.split('_'),
weekStart: 1,
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',
s: 'birkaç saniye',
m: 'bir dakika',
mm: '%d dakika',
h: 'bir saat',
hh: '%d saat',
d: 'bir gün',
dd: '%d gün',
M: 'bir ay',
MM: '%d ay',
y: 'bir yıl',
yy: '%d yıl'
},
ordinal: n => `${n}.`
}
dayjs.locale(locale, null, true)
export default locale
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/src/locale/fr.js | src/locale/fr.js | // French [fr]
import dayjs from 'dayjs'
const locale = {
name: 'fr',
weekdays: 'dimanche_lundi_mardi_mercredi_jeudi_vendredi_samedi'.split('_'),
weekdaysShort: 'dim._lun._mar._mer._jeu._ven._sam.'.split('_'),
weekdaysMin: 'di_lu_ma_me_je_ve_sa'.split('_'),
months: 'janvier_février_mars_avril_mai_juin_juillet_août_septembre_octobre_novembre_décembre'.split('_'),
monthsShort: 'janv._févr._mars_avr._mai_juin_juil._août_sept._oct._nov._déc.'.split('_'),
weekStart: 1,
yearStart: 4,
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',
s: 'quelques secondes',
m: 'une minute',
mm: '%d minutes',
h: 'une heure',
hh: '%d heures',
d: 'un jour',
dd: '%d jours',
M: 'un mois',
MM: '%d mois',
y: 'un an',
yy: '%d ans'
},
ordinal: (n) => {
const o = n === 1 ? 'er' : ''
return `${n}${o}`
}
}
dayjs.locale(locale, null, true)
export default locale
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/src/locale/ml.js | src/locale/ml.js | // Malayalam [ml]
import dayjs from 'dayjs'
const locale = {
name: 'ml',
weekdays: 'ഞായറാഴ്ച_തിങ്കളാഴ്ച_ചൊവ്വാഴ്ച_ബുധനാഴ്ച_വ്യാഴാഴ്ച_വെള്ളിയാഴ്ച_ശനിയാഴ്ച'.split('_'),
months: 'ജനുവരി_ഫെബ്രുവരി_മാർച്ച്_ഏപ്രിൽ_മേയ്_ജൂൺ_ജൂലൈ_ഓഗസ്റ്റ്_സെപ്റ്റംബർ_ഒക്ടോബർ_നവംബർ_ഡിസംബർ'.split('_'),
weekdaysShort: 'ഞായർ_തിങ്കൾ_ചൊവ്വ_ബുധൻ_വ്യാഴം_വെള്ളി_ശനി'.split('_'),
monthsShort: 'ജനു._ഫെബ്രു._മാർ._ഏപ്രി._മേയ്_ജൂൺ_ജൂലൈ._ഓഗ._സെപ്റ്റ._ഒക്ടോ._നവം._ഡിസം.'.split('_'),
weekdaysMin: 'ഞാ_തി_ചൊ_ബു_വ്യാ_വെ_ശ'.split('_'),
ordinal: n => n,
formats: {
LT: 'A h:mm -നു',
LTS: 'A h:mm:ss -നു',
L: 'DD/MM/YYYY',
LL: 'D MMMM YYYY',
LLL: 'D MMMM YYYY, A h:mm -നു',
LLLL: 'dddd, D MMMM YYYY, A h:mm -നു'
},
relativeTime: {
future: '%s കഴിഞ്ഞ്',
past: '%s മുൻപ്',
s: 'അൽപ നിമിഷങ്ങൾ',
m: 'ഒരു മിനിറ്റ്',
mm: '%d മിനിറ്റ്',
h: 'ഒരു മണിക്കൂർ',
hh: '%d മണിക്കൂർ',
d: 'ഒരു ദിവസം',
dd: '%d ദിവസം',
M: 'ഒരു മാസം',
MM: '%d മാസം',
y: 'ഒരു വർഷം',
yy: '%d വർഷം'
}
}
dayjs.locale(locale, null, true)
export default locale
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/src/locale/bi.js | src/locale/bi.js | // Bislama [bi]
import dayjs from 'dayjs'
const locale = {
name: 'bi',
weekdays: 'Sande_Mande_Tusde_Wenesde_Tosde_Fraede_Sarade'.split('_'),
months: 'Januari_Februari_Maj_Eprel_Mei_Jun_Julae_Okis_Septemba_Oktoba_Novemba_Disemba'.split('_'),
weekStart: 1,
weekdaysShort: 'San_Man_Tus_Wen_Tos_Frae_Sar'.split('_'),
monthsShort: 'Jan_Feb_Maj_Epr_Mai_Jun_Jul_Oki_Sep_Okt_Nov_Dis'.split('_'),
weekdaysMin: 'San_Ma_Tu_We_To_Fr_Sar'.split('_'),
ordinal: n => n,
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: 'lo %s',
past: '%s bifo',
s: 'sam seken',
m: 'wan minit',
mm: '%d minit',
h: 'wan haoa',
hh: '%d haoa',
d: 'wan dei',
dd: '%d dei',
M: 'wan manis',
MM: '%d manis',
y: 'wan yia',
yy: '%d yia'
}
}
dayjs.locale(locale, null, true)
export default locale
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/src/locale/be.js | src/locale/be.js | // Belarusian [be]
import dayjs from 'dayjs'
const monthFormat = 'студзеня_лютага_сакавіка_красавіка_траўня_чэрвеня_ліпеня_жніўня_верасня_кастрычніка_лістапада_снежня'.split('_')
const monthStandalone = 'студзень_лютый_сакавік_красавік_травень_чэрвень_ліпень_жнівень_верасень_кастрычнік_лістапад_снежань'.split('_')
const monthShortFormat = 'студ_лют_сак_крас_трав_чэрв_ліп_жнів_вер_каст_ліст_снеж.'.split('_')
const monthShortStandalone = 'студ_лют_сак_крас_трав_чэрв_ліп_жнів_вер_каст_ліст_снеж'.split('_')
const MONTHS_IN_FORMAT = /D[oD]?(\[[^[\]]*\]|\s)+MMMM?/
function plural(word, num) {
const forms = word.split('_')
return num % 10 === 1 && num % 100 !== 11 ? forms[0] : (num % 10 >= 2 && num % 10 <= 4 && (num % 100 < 10 || num % 100 >= 20) ? forms[1] : forms[2]) // eslint-disable-line
}
function relativeTimeWithPlural(number, withoutSuffix, key) {
const format = {
ss: withoutSuffix ? 'секунда_секунды_секунд' : 'секунду_секунды_секунд',
mm: withoutSuffix ? 'хвіліна_хвіліны_хвілін' : 'хвіліну_хвіліны_хвілін',
hh: withoutSuffix ? 'гадзіна_гадзіны_гадзін' : 'гадзіну_гадзіны_гадзін',
dd: 'дзень_дні_дзён',
MM: 'месяц_месяцы_месяцаў',
yy: 'год_гады_гадоў'
}
if (key === 'm') {
return withoutSuffix ? 'хвіліна' : 'хвіліну'
} else if (key === 'h') {
return withoutSuffix ? 'гадзіна' : 'гадзіну'
}
return `${number} ${plural(format[key], +number)}`
}
const months = (dayjsInstance, format) => {
if (MONTHS_IN_FORMAT.test(format)) {
return monthFormat[dayjsInstance.month()]
}
return monthStandalone[dayjsInstance.month()]
}
months.s = monthStandalone
months.f = monthFormat
const monthsShort = (dayjsInstance, format) => {
if (MONTHS_IN_FORMAT.test(format)) {
return monthShortFormat[dayjsInstance.month()]
}
return monthShortStandalone[dayjsInstance.month()]
}
monthsShort.s = monthShortStandalone
monthsShort.f = monthShortFormat
const locale = {
name: 'be',
weekdays: 'нядзеля_панядзелак_аўторак_серада_чацвер_пятніца_субота'.split('_'),
weekdaysShort: 'няд_пнд_аўт_сер_чцв_пят_суб'.split('_'),
weekdaysMin: 'нд_пн_аў_ср_чц_пт_сб'.split('_'),
months,
monthsShort,
weekStart: 1,
yearStart: 4,
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 таму',
s: 'некалькі секунд',
m: relativeTimeWithPlural,
mm: relativeTimeWithPlural,
h: relativeTimeWithPlural,
hh: relativeTimeWithPlural,
d: 'дзень',
dd: relativeTimeWithPlural,
M: 'месяц',
MM: relativeTimeWithPlural,
y: 'год',
yy: relativeTimeWithPlural
},
ordinal: n => n,
meridiem: (hour) => {
if (hour < 4) {
return 'ночы'
} else if (hour < 12) {
return 'раніцы'
} else if (hour < 17) {
return 'дня'
}
return 'вечара'
}
}
dayjs.locale(locale, null, true)
export default locale
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/src/locale/de-at.js | src/locale/de-at.js | // German (Austria) [de-at]
import dayjs from 'dayjs'
const texts = {
s: 'ein paar Sekunden',
m: ['eine Minute', 'einer Minute'],
mm: '%d Minuten',
h: ['eine Stunde', 'einer Stunde'],
hh: '%d Stunden',
d: ['ein Tag', 'einem Tag'],
dd: ['%d Tage', '%d Tagen'],
M: ['ein Monat', 'einem Monat'],
MM: ['%d Monate', '%d Monaten'],
y: ['ein Jahr', 'einem Jahr'],
yy: ['%d Jahre', '%d Jahren']
}
function relativeTimeFormatter(number, withoutSuffix, key) {
let l = texts[key]
if (Array.isArray(l)) {
l = l[withoutSuffix ? 0 : 1]
}
return l.replace('%d', number)
}
const locale = {
name: 'de-at',
weekdays: 'Sonntag_Montag_Dienstag_Mittwoch_Donnerstag_Freitag_Samstag'.split('_'),
weekdaysShort: 'So._Mo._Di._Mi._Do._Fr._Sa.'.split('_'),
weekdaysMin: 'So_Mo_Di_Mi_Do_Fr_Sa'.split('_'),
months: 'Jänner_Februar_März_April_Mai_Juni_Juli_August_September_Oktober_November_Dezember'.split('_'),
monthsShort: 'Jän._Feb._März_Apr._Mai_Juni_Juli_Aug._Sep._Okt._Nov._Dez.'.split('_'),
ordinal: n => `${n}.`,
weekStart: 1,
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',
s: relativeTimeFormatter,
m: relativeTimeFormatter,
mm: relativeTimeFormatter,
h: relativeTimeFormatter,
hh: relativeTimeFormatter,
d: relativeTimeFormatter,
dd: relativeTimeFormatter,
M: relativeTimeFormatter,
MM: relativeTimeFormatter,
y: relativeTimeFormatter,
yy: relativeTimeFormatter
}
}
dayjs.locale(locale, null, true)
export default locale
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/src/locale/ga.js | src/locale/ga.js | // Irish or Irish Gaelic [ga]
import dayjs from 'dayjs'
const locale = {
name: 'ga',
weekdays: 'Dé Domhnaigh_Dé Luain_Dé Máirt_Dé Céadaoin_Déardaoin_Dé hAoine_Dé Sathairn'.split('_'),
months: 'Eanáir_Feabhra_Márta_Aibreán_Bealtaine_Meitheamh_Iúil_Lúnasa_Meán Fómhair_Deireadh Fómhair_Samhain_Nollaig'.split('_'),
weekStart: 1,
weekdaysShort: 'Dom_Lua_Mái_Céa_Déa_Aoi_Sat'.split('_'),
monthsShort: 'Ean_Fea_Már_Aib_Beal_Mei_Iúil_Lún_MFómh_DFómh_Samh_Noll'.split('_'),
weekdaysMin: 'Do_Lu_Má_Cé_Dé_Ao_Sa'.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: 'i %s',
past: '%s ó shin',
s: 'cúpla soicind',
m: 'nóiméad',
mm: '%d nóiméad',
h: 'uair an chloig',
hh: '%d uair an chloig',
d: 'lá',
dd: '%d lá',
M: 'mí',
MM: '%d mí',
y: 'bliain',
yy: '%d bliain'
}
}
dayjs.locale(locale, null, true)
export default locale
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/src/locale/rn.js | src/locale/rn.js | // Kirundi [rn]
import dayjs from 'dayjs'
const locale = {
name: 'rn',
weekdays: 'Ku wa Mungu_Ku wa Mbere_Ku wa Kabiri_Ku wa Gatatu_Ku wa Kane_Ku wa Gatanu_Ku wa Gatandatu'.split('_'),
weekdaysShort: 'Kngu_Kmbr_Kbri_Ktat_Kkan_Ktan_Kdat'.split('_'),
weekdaysMin: 'K7_K1_K2_K3_K4_K5_K6'.split('_'),
months: 'Nzero_Ruhuhuma_Ntwarante_Ndamukiza_Rusama_Ruhenshi_Mukakaro_Myandagaro_Nyakanga_Gitugutu_Munyonyo_Kigarama'.split('_'),
monthsShort: 'Nzer_Ruhuh_Ntwar_Ndam_Rus_Ruhen_Muk_Myand_Nyak_Git_Muny_Kig'.split('_'),
weekStart: 1,
ordinal: n => n,
relativeTime: {
future: 'mu %s',
past: '%s',
s: 'amasegonda',
m: 'Umunota',
mm: '%d iminota',
h: 'isaha',
hh: '%d amasaha',
d: 'Umunsi',
dd: '%d iminsi',
M: 'ukwezi',
MM: '%d amezi',
y: 'umwaka',
yy: '%d imyaka'
},
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'
}
}
dayjs.locale(locale, null, true)
export default locale
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/src/locale/km.js | src/locale/km.js | // Cambodian [km]
import dayjs from 'dayjs'
const locale = {
name: 'km',
weekdays: 'អាទិត្យ_ច័ន្ទ_អង្គារ_ពុធ_ព្រហស្បតិ៍_សុក្រ_សៅរ៍'.split('_'),
months: 'មករា_កុម្ភៈ_មីនា_មេសា_ឧសភា_មិថុនា_កក្កដា_សីហា_កញ្ញា_តុលា_វិច្ឆិកា_ធ្នូ'.split('_'),
weekStart: 1,
weekdaysShort: 'អា_ច_អ_ព_ព្រ_សុ_ស'.split('_'),
monthsShort: 'មករា_កុម្ភៈ_មីនា_មេសា_ឧសភា_មិថុនា_កក្កដា_សីហា_កញ្ញា_តុលា_វិច្ឆិកា_ធ្នូ'.split('_'),
weekdaysMin: 'អា_ច_អ_ព_ព្រ_សុ_ស'.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មុន',
s: 'ប៉ុន្មានវិនាទី',
m: 'មួយនាទី',
mm: '%d នាទី',
h: 'មួយម៉ោង',
hh: '%d ម៉ោង',
d: 'មួយថ្ងៃ',
dd: '%d ថ្ងៃ',
M: 'មួយខែ',
MM: '%d ខែ',
y: 'មួយឆ្នាំ',
yy: '%d ឆ្នាំ'
}
}
dayjs.locale(locale, null, true)
export default locale
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/src/locale/en-il.js | src/locale/en-il.js | // English (Israel) [en-il]
import dayjs from 'dayjs'
const locale = {
name: 'en-il',
weekdays: 'Sunday_Monday_Tuesday_Wednesday_Thursday_Friday_Saturday'.split('_'),
months: 'January_February_March_April_May_June_July_August_September_October_November_December'.split('_'),
weekdaysShort: 'Sun_Mon_Tue_Wed_Thu_Fri_Sat'.split('_'),
monthsShort: 'Jan_Feb_Mar_Apr_May_Jun_Jul_Aug_Sep_Oct_Nov_Dec'.split('_'),
weekdaysMin: 'Su_Mo_Tu_We_Th_Fr_Sa'.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: 'in %s',
past: '%s ago',
s: 'a few seconds',
m: 'a minute',
mm: '%d minutes',
h: 'an hour',
hh: '%d hours',
d: 'a day',
dd: '%d days',
M: 'a month',
MM: '%d months',
y: 'a year',
yy: '%d years'
}
}
dayjs.locale(locale, null, true)
export default locale
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/src/locale/he.js | src/locale/he.js | // Hebrew [he]
import dayjs from 'dayjs'
const texts = {
s: 'מספר שניות',
ss: '%d שניות',
m: 'דקה',
mm: '%d דקות',
h: 'שעה',
hh: '%d שעות',
hh2: 'שעתיים',
d: 'יום',
dd: '%d ימים',
dd2: 'יומיים',
M: 'חודש',
MM: '%d חודשים',
MM2: 'חודשיים',
y: 'שנה',
yy: '%d שנים',
yy2: 'שנתיים'
}
function relativeTimeFormatter(number, withoutSuffix, key) {
const text = texts[key + (number === 2 ? '2' : '')] || texts[key]
return text.replace('%d', number)
}
const locale = {
name: 'he',
weekdays: 'ראשון_שני_שלישי_רביעי_חמישי_שישי_שבת'.split('_'),
weekdaysShort: 'א׳_ב׳_ג׳_ד׳_ה׳_ו׳_ש׳'.split('_'),
weekdaysMin: 'א׳_ב׳_ג׳_ד׳_ה׳_ו_ש׳'.split('_'),
months: 'ינואר_פברואר_מרץ_אפריל_מאי_יוני_יולי_אוגוסט_ספטמבר_אוקטובר_נובמבר_דצמבר'.split('_'),
monthsShort: 'ינו_פבר_מרץ_אפר_מאי_יונ_יול_אוג_ספט_אוק_נוב_דצמ'.split('_'),
relativeTime: {
future: 'בעוד %s',
past: 'לפני %s',
s: relativeTimeFormatter,
m: relativeTimeFormatter,
mm: relativeTimeFormatter,
h: relativeTimeFormatter,
hh: relativeTimeFormatter,
d: relativeTimeFormatter,
dd: relativeTimeFormatter,
M: relativeTimeFormatter,
MM: relativeTimeFormatter,
y: relativeTimeFormatter,
yy: relativeTimeFormatter
},
ordinal: n => n,
format: {
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',
l: 'D/M/YYYY',
ll: 'D MMM YYYY',
lll: 'D MMM YYYY HH:mm',
llll: 'ddd, D MMM YYYY HH:mm'
},
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',
l: 'D/M/YYYY',
ll: 'D MMM YYYY',
lll: 'D MMM YYYY HH:mm',
llll: 'ddd, D MMM YYYY HH:mm'
}
}
dayjs.locale(locale, null, true)
export default locale
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/src/locale/pl.js | src/locale/pl.js | // Polish [pl]
import dayjs from 'dayjs'
function plural(n) {
return (n % 10 < 5) && (n % 10 > 1) && ((~~(n / 10) % 10) !== 1) // eslint-disable-line
}
/* eslint-disable */
function translate(number, withoutSuffix, key) {
const result = `${number} `
switch (key) {
case 'm':
return withoutSuffix ? 'minuta' : 'minutę'
case 'mm':
return result + (plural(number) ? 'minuty' : 'minut')
case 'h':
return withoutSuffix ? 'godzina' : 'godzinę'
case 'hh':
return result + (plural(number) ? 'godziny' : 'godzin')
case 'MM':
return result + (plural(number) ? 'miesiące' : 'miesięcy')
case 'yy':
return result + (plural(number) ? 'lata' : 'lat')
}
}
/* eslint-enable */
const monthFormat = 'stycznia_lutego_marca_kwietnia_maja_czerwca_lipca_sierpnia_września_października_listopada_grudnia'.split('_')
const monthStandalone = 'styczeń_luty_marzec_kwiecień_maj_czerwiec_lipiec_sierpień_wrzesień_październik_listopad_grudzień'.split('_')
const MONTHS_IN_FORMAT = /D MMMM/
const months = (dayjsInstance, format) => {
if (MONTHS_IN_FORMAT.test(format)) {
return monthFormat[dayjsInstance.month()]
}
return monthStandalone[dayjsInstance.month()]
}
months.s = monthStandalone
months.f = monthFormat
const locale = {
name: 'pl',
weekdays: 'niedziela_poniedziałek_wtorek_środa_czwartek_piątek_sobota'.split('_'),
weekdaysShort: 'ndz_pon_wt_śr_czw_pt_sob'.split('_'),
weekdaysMin: 'Nd_Pn_Wt_Śr_Cz_Pt_So'.split('_'),
months,
monthsShort: 'sty_lut_mar_kwi_maj_cze_lip_sie_wrz_paź_lis_gru'.split('_'),
ordinal: n => `${n}.`,
weekStart: 1,
yearStart: 4,
relativeTime: {
future: 'za %s',
past: '%s temu',
s: 'kilka sekund',
m: translate,
mm: translate,
h: translate,
hh: translate,
d: '1 dzień',
dd: '%d dni',
M: 'miesiąc',
MM: translate,
y: 'rok',
yy: translate
},
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'
}
}
dayjs.locale(locale, null, true)
export default locale
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/src/locale/ms.js | src/locale/ms.js | // Malay [ms]
import dayjs from 'dayjs'
const locale = {
name: 'ms',
weekdays: 'Ahad_Isnin_Selasa_Rabu_Khamis_Jumaat_Sabtu'.split('_'),
weekdaysShort: 'Ahd_Isn_Sel_Rab_Kha_Jum_Sab'.split('_'),
weekdaysMin: 'Ah_Is_Sl_Rb_Km_Jm_Sb'.split('_'),
months: 'Januari_Februari_Mac_April_Mei_Jun_Julai_Ogos_September_Oktober_November_Disember'.split('_'),
monthsShort: 'Jan_Feb_Mac_Apr_Mei_Jun_Jul_Ogs_Sep_Okt_Nov_Dis'.split('_'),
weekStart: 1,
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: 'dalam %s',
past: '%s yang lepas',
s: 'beberapa saat',
m: 'seminit',
mm: '%d minit',
h: 'sejam',
hh: '%d jam',
d: 'sehari',
dd: '%d hari',
M: 'sebulan',
MM: '%d bulan',
y: 'setahun',
yy: '%d tahun'
},
ordinal: n => `${n}.`
}
dayjs.locale(locale, null, true)
export default locale
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/src/locale/ur.js | src/locale/ur.js | // Urdu [ur]
import dayjs from 'dayjs'
const locale = {
name: 'ur',
weekdays: 'اتوار_پیر_منگل_بدھ_جمعرات_جمعہ_ہفتہ'.split('_'),
months: 'جنوری_فروری_مارچ_اپریل_مئی_جون_جولائی_اگست_ستمبر_اکتوبر_نومبر_دسمبر'.split('_'),
weekStart: 1,
weekdaysShort: 'اتوار_پیر_منگل_بدھ_جمعرات_جمعہ_ہفتہ'.split('_'),
monthsShort: 'جنوری_فروری_مارچ_اپریل_مئی_جون_جولائی_اگست_ستمبر_اکتوبر_نومبر_دسمبر'.split('_'),
weekdaysMin: 'اتوار_پیر_منگل_بدھ_جمعرات_جمعہ_ہفتہ'.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 قبل',
s: 'چند سیکنڈ',
m: 'ایک منٹ',
mm: '%d منٹ',
h: 'ایک گھنٹہ',
hh: '%d گھنٹے',
d: 'ایک دن',
dd: '%d دن',
M: 'ایک ماہ',
MM: '%d ماہ',
y: 'ایک سال',
yy: '%d سال'
}
}
dayjs.locale(locale, null, true)
export default locale
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/src/locale/en-ie.js | src/locale/en-ie.js | // English (Ireland) [en-ie]
import dayjs from 'dayjs'
const locale = {
name: 'en-ie',
weekdays: 'Sunday_Monday_Tuesday_Wednesday_Thursday_Friday_Saturday'.split('_'),
months: 'January_February_March_April_May_June_July_August_September_October_November_December'.split('_'),
weekStart: 1,
weekdaysShort: 'Sun_Mon_Tue_Wed_Thu_Fri_Sat'.split('_'),
monthsShort: 'Jan_Feb_Mar_Apr_May_Jun_Jul_Aug_Sep_Oct_Nov_Dec'.split('_'),
weekdaysMin: 'Su_Mo_Tu_We_Th_Fr_Sa'.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: 'in %s',
past: '%s ago',
s: 'a few seconds',
m: 'a minute',
mm: '%d minutes',
h: 'an hour',
hh: '%d hours',
d: 'a day',
dd: '%d days',
M: 'a month',
MM: '%d months',
y: 'a year',
yy: '%d years'
}
}
dayjs.locale(locale, null, true)
export default locale
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/src/locale/cs.js | src/locale/cs.js | // Czech [cs]
import dayjs from 'dayjs'
function plural(n) {
return (n > 1) && (n < 5) && (~~(n / 10) !== 1) // eslint-disable-line
}
/* eslint-disable */
function translate(number, withoutSuffix, key, isFuture) {
const result = `${number} `
switch (key) {
case 's': // a few seconds / in a few seconds / a few seconds ago
return (withoutSuffix || isFuture) ? 'pár sekund' : 'pár sekundami'
case 'm': // a minute / in a minute / a minute ago
return withoutSuffix ? 'minuta' : (isFuture ? 'minutu' : 'minutou')
case 'mm': // 9 minutes / in 9 minutes / 9 minutes ago
if (withoutSuffix || isFuture) {
return result + (plural(number) ? 'minuty' : 'minut')
}
return `${result}minutami`
case 'h': // an hour / in an hour / an hour ago
return withoutSuffix ? 'hodina' : (isFuture ? 'hodinu' : 'hodinou')
case 'hh': // 9 hours / in 9 hours / 9 hours ago
if (withoutSuffix || isFuture) {
return result + (plural(number) ? 'hodiny' : 'hodin')
}
return `${result}hodinami`
case 'd': // a day / in a day / a day ago
return (withoutSuffix || isFuture) ? 'den' : 'dnem'
case 'dd': // 9 days / in 9 days / 9 days ago
if (withoutSuffix || isFuture) {
return result + (plural(number) ? 'dny' : 'dní')
}
return `${result}dny`
case 'M': // a month / in a month / a month ago
return (withoutSuffix || isFuture) ? 'měsíc' : 'měsícem'
case 'MM': // 9 months / in 9 months / 9 months ago
if (withoutSuffix || isFuture) {
return result + (plural(number) ? 'měsíce' : 'měsíců')
}
return `${result}měsíci`
case 'y': // a year / in a year / a year ago
return (withoutSuffix || isFuture) ? 'rok' : 'rokem'
case 'yy': // 9 years / in 9 years / 9 years ago
if (withoutSuffix || isFuture) {
return result + (plural(number) ? 'roky' : 'let')
}
return `${result}lety`
}
}
/* eslint-enable */
const locale = {
name: 'cs',
weekdays: 'neděle_pondělí_úterý_středa_čtvrtek_pátek_sobota'.split('_'),
weekdaysShort: 'ne_po_út_st_čt_pá_so'.split('_'),
weekdaysMin: 'ne_po_út_st_čt_pá_so'.split('_'),
months: 'leden_únor_březen_duben_květen_červen_červenec_srpen_září_říjen_listopad_prosinec'.split('_'),
monthsShort: 'led_úno_bře_dub_kvě_čvn_čvc_srp_zář_říj_lis_pro'.split('_'),
weekStart: 1,
yearStart: 4,
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',
l: 'D. M. YYYY'
},
relativeTime: {
future: 'za %s',
past: 'před %s',
s: translate,
m: translate,
mm: translate,
h: translate,
hh: translate,
d: translate,
dd: translate,
M: translate,
MM: translate,
y: translate,
yy: translate
}
}
dayjs.locale(locale, null, true)
export default locale
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/src/locale/ar-kw.js | src/locale/ar-kw.js | // Arabic (Kuwait) [ar-kw]
import dayjs from 'dayjs'
const locale = {
name: 'ar-kw',
weekdays: 'الأحد_الإثنين_الثلاثاء_الأربعاء_الخميس_الجمعة_السبت'.split('_'),
months: 'يناير_فبراير_مارس_أبريل_ماي_يونيو_يوليوز_غشت_شتنبر_أكتوبر_نونبر_دجنبر'.split('_'),
weekdaysShort: 'احد_اثنين_ثلاثاء_اربعاء_خميس_جمعة_سبت'.split('_'),
monthsShort: 'يناير_فبراير_مارس_أبريل_ماي_يونيو_يوليوز_غشت_شتنبر_أكتوبر_نونبر_دجنبر'.split('_'),
weekdaysMin: 'ح_ن_ث_ر_خ_ج_س'.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'
},
meridiem: hour => (hour > 12 ? 'م' : 'ص'),
relativeTime: {
future: 'في %s',
past: 'منذ %s',
s: 'ثوان',
m: 'دقيقة',
mm: '%d دقائق',
h: 'ساعة',
hh: '%d ساعات',
d: 'يوم',
dd: '%d أيام',
M: 'شهر',
MM: '%d أشهر',
y: 'سنة',
yy: '%d سنوات'
}
}
dayjs.locale(locale, null, true)
export default locale
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/src/locale/en-in.js | src/locale/en-in.js | // English (India) [en-in]
import dayjs from 'dayjs'
const locale = {
name: 'en-in',
weekdays: 'Sunday_Monday_Tuesday_Wednesday_Thursday_Friday_Saturday'.split('_'),
weekdaysShort: 'Sun_Mon_Tue_Wed_Thu_Fri_Sat'.split('_'),
weekdaysMin: 'Su_Mo_Tu_We_Th_Fr_Sa'.split('_'),
months: 'January_February_March_April_May_June_July_August_September_October_November_December'.split('_'),
monthsShort: 'Jan_Feb_Mar_Apr_May_Jun_Jul_Aug_Sep_Oct_Nov_Dec'.split('_'),
weekStart: 1,
yearStart: 4,
relativeTime: {
future: 'in %s',
past: '%s ago',
s: 'a few seconds',
m: 'a minute',
mm: '%d minutes',
h: 'an hour',
hh: '%d hours',
d: 'a day',
dd: '%d days',
M: 'a month',
MM: '%d months',
y: 'a year',
yy: '%d years'
},
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'
},
ordinal: (n) => {
const s = ['th', 'st', 'nd', 'rd']
const v = n % 100
return `[${n}${(s[(v - 20) % 10] || s[v] || s[0])}]`
}
}
dayjs.locale(locale, null, true)
export default locale
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/src/locale/ar-dz.js | src/locale/ar-dz.js | // Arabic (Algeria) [ar-dz]
import dayjs from 'dayjs'
const locale = {
name: 'ar-dz',
weekdays: 'الأحد_الإثنين_الثلاثاء_الأربعاء_الخميس_الجمعة_السبت'.split('_'),
months: 'جانفي_فيفري_مارس_أفريل_ماي_جوان_جويلية_أوت_سبتمبر_أكتوبر_نوفمبر_ديسمبر'.split('_'),
weekdaysShort: 'احد_اثنين_ثلاثاء_اربعاء_خميس_جمعة_سبت'.split('_'),
monthsShort: 'جانفي_فيفري_مارس_أفريل_ماي_جوان_جويلية_أوت_سبتمبر_أكتوبر_نوفمبر_ديسمبر'.split('_'),
weekdaysMin: 'أح_إث_ثلا_أر_خم_جم_سب'.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'
},
meridiem: hour => (hour > 12 ? 'م' : 'ص'),
relativeTime: {
future: 'في %s',
past: 'منذ %s',
s: 'ثوان',
m: 'دقيقة',
mm: '%d دقائق',
h: 'ساعة',
hh: '%d ساعات',
d: 'يوم',
dd: '%d أيام',
M: 'شهر',
MM: '%d أشهر',
y: 'سنة',
yy: '%d سنوات'
}
}
dayjs.locale(locale, null, true)
export default locale
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/src/locale/en-nz.js | src/locale/en-nz.js | // English (New Zealand) [en-nz]
import dayjs from 'dayjs'
const locale = {
name: 'en-nz',
weekdays: 'Sunday_Monday_Tuesday_Wednesday_Thursday_Friday_Saturday'.split('_'),
months: 'January_February_March_April_May_June_July_August_September_October_November_December'.split('_'),
weekStart: 1,
weekdaysShort: 'Sun_Mon_Tue_Wed_Thu_Fri_Sat'.split('_'),
monthsShort: 'Jan_Feb_Mar_Apr_May_Jun_Jul_Aug_Sep_Oct_Nov_Dec'.split('_'),
weekdaysMin: 'Su_Mo_Tu_We_Th_Fr_Sa'.split('_'),
ordinal: (n) => {
const s = ['th', 'st', 'nd', 'rd']
const v = n % 100
return `[${n}${s[(v - 20) % 10] || s[v] || s[0]}]`
},
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: 'in %s',
past: '%s ago',
s: 'a few seconds',
m: 'a minute',
mm: '%d minutes',
h: 'an hour',
hh: '%d hours',
d: 'a day',
dd: '%d days',
M: 'a month',
MM: '%d months',
y: 'a year',
yy: '%d years'
}
}
dayjs.locale(locale, null, true)
export default locale
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/src/locale/ug-cn.js | src/locale/ug-cn.js | // Uyghur (China) [ug-cn]
import dayjs from 'dayjs'
const locale = {
name: 'ug-cn',
weekdays: 'يەكشەنبە_دۈشەنبە_سەيشەنبە_چارشەنبە_پەيشەنبە_جۈمە_شەنبە'.split('_'),
months: 'يانۋار_فېۋرال_مارت_ئاپرېل_ماي_ئىيۇن_ئىيۇل_ئاۋغۇست_سېنتەبىر_ئۆكتەبىر_نويابىر_دېكابىر'.split('_'),
weekStart: 1,
weekdaysShort: 'يە_دۈ_سە_چا_پە_جۈ_شە'.split('_'),
monthsShort: 'يانۋار_فېۋرال_مارت_ئاپرېل_ماي_ئىيۇن_ئىيۇل_ئاۋغۇست_سېنتەبىر_ئۆكتەبىر_نويابىر_دېكابىر'.split('_'),
weekdaysMin: 'يە_دۈ_سە_چا_پە_جۈ_شە'.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: 'dddd، YYYY-يىلىM-ئاينىڭD-كۈنى، HH:mm'
},
relativeTime: {
future: '%s كېيىن',
past: '%s بۇرۇن',
s: 'نەچچە سېكونت',
m: 'بىر مىنۇت',
mm: '%d مىنۇت',
h: 'بىر سائەت',
hh: '%d سائەت',
d: 'بىر كۈن',
dd: '%d كۈن',
M: 'بىر ئاي',
MM: '%d ئاي',
y: 'بىر يىل',
yy: '%d يىل'
}
}
dayjs.locale(locale, null, true)
export default locale
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/src/locale/gd.js | src/locale/gd.js | // Scottish Gaelic [gd]
import dayjs from 'dayjs'
const locale = {
name: 'gd',
weekdays: 'Didòmhnaich_Diluain_Dimàirt_Diciadain_Diardaoin_Dihaoine_Disathairne'.split('_'),
months: 'Am Faoilleach_An Gearran_Am Màrt_An Giblean_An Cèitean_An t-Ògmhios_An t-Iuchar_An Lùnastal_An t-Sultain_An Dàmhair_An t-Samhain_An Dùbhlachd'.split('_'),
weekStart: 1,
weekdaysShort: 'Did_Dil_Dim_Dic_Dia_Dih_Dis'.split('_'),
monthsShort: 'Faoi_Gear_Màrt_Gibl_Cèit_Ògmh_Iuch_Lùn_Sult_Dàmh_Samh_Dùbh'.split('_'),
weekdaysMin: 'Dò_Lu_Mà_Ci_Ar_Ha_Sa'.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: 'ann an %s',
past: 'bho chionn %s',
s: 'beagan diogan',
m: 'mionaid',
mm: '%d mionaidean',
h: 'uair',
hh: '%d uairean',
d: 'latha',
dd: '%d latha',
M: 'mìos',
MM: '%d mìosan',
y: 'bliadhna',
yy: '%d bliadhna'
}
}
dayjs.locale(locale, null, true)
export default locale
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/src/locale/ru.js | src/locale/ru.js | // Russian [ru]
import dayjs from 'dayjs'
const monthFormat = 'января_февраля_марта_апреля_мая_июня_июля_августа_сентября_октября_ноября_декабря'.split('_')
const monthStandalone = 'январь_февраль_март_апрель_май_июнь_июль_август_сентябрь_октябрь_ноябрь_декабрь'.split('_')
const monthShortFormat = 'янв._февр._мар._апр._мая_июня_июля_авг._сент._окт._нояб._дек.'.split('_')
const monthShortStandalone = 'янв._февр._март_апр._май_июнь_июль_авг._сент._окт._нояб._дек.'.split('_')
const MONTHS_IN_FORMAT = /D[oD]?(\[[^[\]]*\]|\s)+MMMM?/
function plural(word, num) {
const forms = word.split('_')
return num % 10 === 1 && num % 100 !== 11 ? forms[0] : (num % 10 >= 2 && num % 10 <= 4 && (num % 100 < 10 || num % 100 >= 20) ? forms[1] : forms[2]) // eslint-disable-line
}
function relativeTimeWithPlural(number, withoutSuffix, key) {
const format = {
mm: withoutSuffix ? 'минута_минуты_минут' : 'минуту_минуты_минут',
hh: 'час_часа_часов',
dd: 'день_дня_дней',
MM: 'месяц_месяца_месяцев',
yy: 'год_года_лет'
}
if (key === 'm') {
return withoutSuffix ? 'минута' : 'минуту'
}
return `${number} ${plural(format[key], +number)}`
}
const months = (dayjsInstance, format) => {
if (MONTHS_IN_FORMAT.test(format)) {
return monthFormat[dayjsInstance.month()]
}
return monthStandalone[dayjsInstance.month()]
}
months.s = monthStandalone
months.f = monthFormat
const monthsShort = (dayjsInstance, format) => {
if (MONTHS_IN_FORMAT.test(format)) {
return monthShortFormat[dayjsInstance.month()]
}
return monthShortStandalone[dayjsInstance.month()]
}
monthsShort.s = monthShortStandalone
monthsShort.f = monthShortFormat
const locale = {
name: 'ru',
weekdays: 'воскресенье_понедельник_вторник_среда_четверг_пятница_суббота'.split('_'),
weekdaysShort: 'вск_пнд_втр_срд_чтв_птн_сбт'.split('_'),
weekdaysMin: 'вс_пн_вт_ср_чт_пт_сб'.split('_'),
months,
monthsShort,
weekStart: 1,
yearStart: 4,
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 назад',
s: 'несколько секунд',
m: relativeTimeWithPlural,
mm: relativeTimeWithPlural,
h: 'час',
hh: relativeTimeWithPlural,
d: 'день',
dd: relativeTimeWithPlural,
M: 'месяц',
MM: relativeTimeWithPlural,
y: 'год',
yy: relativeTimeWithPlural
},
ordinal: n => n,
meridiem: (hour) => {
if (hour < 4) {
return 'ночи'
} else if (hour < 12) {
return 'утра'
} else if (hour < 17) {
return 'дня'
}
return 'вечера'
}
}
dayjs.locale(locale, null, true)
export default locale
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/src/locale/fi.js | src/locale/fi.js | // Finnish [fi]
import dayjs from 'dayjs'
function relativeTimeFormatter(number, withoutSuffix, key, isFuture) {
const past = {
s: 'muutama sekunti',
m: 'minuutti',
mm: '%d minuuttia',
h: 'tunti',
hh: '%d tuntia',
d: 'päivä',
dd: '%d päivää',
M: 'kuukausi',
MM: '%d kuukautta',
y: 'vuosi',
yy: '%d vuotta',
numbers: 'nolla_yksi_kaksi_kolme_neljä_viisi_kuusi_seitsemän_kahdeksan_yhdeksän'.split('_')
}
const future = {
s: 'muutaman sekunnin',
m: 'minuutin',
mm: '%d minuutin',
h: 'tunnin',
hh: '%d tunnin',
d: 'päivän',
dd: '%d päivän',
M: 'kuukauden',
MM: '%d kuukauden',
y: 'vuoden',
yy: '%d vuoden',
numbers: 'nollan_yhden_kahden_kolmen_neljän_viiden_kuuden_seitsemän_kahdeksan_yhdeksän'.split('_')
}
const words = (isFuture && !withoutSuffix) ? future : past
const result = words[key]
if (number < 10) {
return result.replace('%d', words.numbers[number])
}
return result.replace('%d', number)
}
const locale = {
name: 'fi', // Finnish
weekdays: 'sunnuntai_maanantai_tiistai_keskiviikko_torstai_perjantai_lauantai'.split('_'), // Note weekdays are not capitalized in Finnish
weekdaysShort: 'su_ma_ti_ke_to_pe_la'.split('_'), // There is no short form of weekdays in Finnish except this 2 letter format so it is used for both 'weekdaysShort' and 'weekdaysMin'
weekdaysMin: 'su_ma_ti_ke_to_pe_la'.split('_'),
months: 'tammikuu_helmikuu_maaliskuu_huhtikuu_toukokuu_kesäkuu_heinäkuu_elokuu_syyskuu_lokakuu_marraskuu_joulukuu'.split('_'), // Note month names are not capitalized in Finnish
monthsShort: 'tammi_helmi_maalis_huhti_touko_kesä_heinä_elo_syys_loka_marras_joulu'.split('_'),
ordinal: n => `${n}.`,
weekStart: 1,
yearStart: 4,
relativeTime: {
future: '%s päästä',
past: '%s sitten',
s: relativeTimeFormatter,
m: relativeTimeFormatter,
mm: relativeTimeFormatter,
h: relativeTimeFormatter,
hh: relativeTimeFormatter,
d: relativeTimeFormatter,
dd: relativeTimeFormatter,
M: relativeTimeFormatter,
MM: relativeTimeFormatter,
y: relativeTimeFormatter,
yy: relativeTimeFormatter
},
formats: {
LT: 'HH.mm',
LTS: 'HH.mm.ss',
L: 'DD.MM.YYYY',
LL: 'D. MMMM[ta] YYYY',
LLL: 'D. MMMM[ta] YYYY, [klo] HH.mm',
LLLL: 'dddd, D. MMMM[ta] YYYY, [klo] HH.mm',
l: 'D.M.YYYY',
ll: 'D. MMM YYYY',
lll: 'D. MMM YYYY, [klo] HH.mm',
llll: 'ddd, D. MMM YYYY, [klo] HH.mm'
}
}
dayjs.locale(locale, null, true)
export default locale
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/src/locale/et.js | src/locale/et.js | // Estonian [et]
import dayjs from 'dayjs'
function relativeTimeWithTense(number, withoutSuffix, key, isFuture) {
const format = {
s: ['mõne sekundi', 'mõni sekund', 'paar sekundit'],
m: ['ühe minuti', 'üks minut'],
mm: ['%d minuti', '%d minutit'],
h: ['ühe tunni', 'tund aega', 'üks tund'],
hh: ['%d tunni', '%d tundi'],
d: ['ühe päeva', 'üks päev'],
M: ['kuu aja', 'kuu aega', 'üks kuu'],
MM: ['%d kuu', '%d kuud'],
y: ['ühe aasta', 'aasta', 'üks aasta'],
yy: ['%d aasta', '%d aastat']
}
if (withoutSuffix) {
return (format[key][2] ? format[key][2] : format[key][1]).replace('%d', number)
}
return (isFuture ? format[key][0] : format[key][1]).replace('%d', number)
}
const locale = {
name: 'et', // Estonian
weekdays: 'pühapäev_esmaspäev_teisipäev_kolmapäev_neljapäev_reede_laupäev'.split('_'), // Note weekdays are not capitalized in Estonian
weekdaysShort: 'P_E_T_K_N_R_L'.split('_'), // There is no short form of weekdays in Estonian except this 1 letter format so it is used for both 'weekdaysShort' and 'weekdaysMin'
weekdaysMin: 'P_E_T_K_N_R_L'.split('_'),
months: 'jaanuar_veebruar_märts_aprill_mai_juuni_juuli_august_september_oktoober_november_detsember'.split('_'), // Note month names are not capitalized in Estonian
monthsShort: 'jaan_veebr_märts_apr_mai_juuni_juuli_aug_sept_okt_nov_dets'.split('_'),
ordinal: n => `${n}.`,
weekStart: 1,
relativeTime: {
future: '%s pärast',
past: '%s tagasi',
s: relativeTimeWithTense,
m: relativeTimeWithTense,
mm: relativeTimeWithTense,
h: relativeTimeWithTense,
hh: relativeTimeWithTense,
d: relativeTimeWithTense,
dd: '%d päeva',
M: relativeTimeWithTense,
MM: relativeTimeWithTense,
y: relativeTimeWithTense,
yy: relativeTimeWithTense
},
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'
}
}
dayjs.locale(locale, null, true)
export default locale
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/src/locale/sk.js | src/locale/sk.js | // Slovak [sk]
import dayjs from 'dayjs'
function plural(n) {
return (n > 1) && (n < 5) && (~~(n / 10) !== 1) // eslint-disable-line
}
/* eslint-disable */
function translate(number, withoutSuffix, key, isFuture) {
const result = `${number} `
switch (key) {
case 's': // a few seconds / in a few seconds / a few seconds ago
return (withoutSuffix || isFuture) ? 'pár sekúnd' : 'pár sekundami'
case 'm': // a minute / in a minute / a minute ago
return withoutSuffix ? 'minúta' : (isFuture ? 'minútu' : 'minútou')
case 'mm': // 9 minutes / in 9 minutes / 9 minutes ago
if (withoutSuffix || isFuture) {
return result + (plural(number) ? 'minúty' : 'minút')
}
return `${result}minútami`
case 'h': // an hour / in an hour / an hour ago
return withoutSuffix ? 'hodina' : (isFuture ? 'hodinu' : 'hodinou')
case 'hh': // 9 hours / in 9 hours / 9 hours ago
if (withoutSuffix || isFuture) {
return result + (plural(number) ? 'hodiny' : 'hodín')
}
return `${result}hodinami`
case 'd': // a day / in a day / a day ago
return (withoutSuffix || isFuture) ? 'deň' : 'dňom'
case 'dd': // 9 days / in 9 days / 9 days ago
if (withoutSuffix || isFuture) {
return result + (plural(number) ? 'dni' : 'dní')
}
return `${result}dňami`
case 'M': // a month / in a month / a month ago
return (withoutSuffix || isFuture) ? 'mesiac' : 'mesiacom'
case 'MM': // 9 months / in 9 months / 9 months ago
if (withoutSuffix || isFuture) {
return result + (plural(number) ? 'mesiace' : 'mesiacov')
}
return `${result}mesiacmi`
case 'y': // a year / in a year / a year ago
return (withoutSuffix || isFuture) ? 'rok' : 'rokom'
case 'yy': // 9 years / in 9 years / 9 years ago
if (withoutSuffix || isFuture) {
return result + (plural(number) ? 'roky' : 'rokov')
}
return `${result}rokmi`
}
}
/* eslint-enable */
const locale = {
name: 'sk',
weekdays: 'nedeľa_pondelok_utorok_streda_štvrtok_piatok_sobota'.split('_'),
weekdaysShort: 'ne_po_ut_st_št_pi_so'.split('_'),
weekdaysMin: 'ne_po_ut_st_št_pi_so'.split('_'),
months: 'január_február_marec_apríl_máj_jún_júl_august_september_október_november_december'.split('_'),
monthsShort: 'jan_feb_mar_apr_máj_jún_júl_aug_sep_okt_nov_dec'.split('_'),
weekStart: 1,
yearStart: 4,
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',
l: 'D. M. YYYY'
},
relativeTime: {
future: 'za %s', // Should be `o %s` (change when moment/moment#5408 is fixed)
past: 'pred %s',
s: translate,
m: translate,
mm: translate,
h: translate,
hh: translate,
d: translate,
dd: translate,
M: translate,
MM: translate,
y: translate,
yy: translate
}
}
dayjs.locale(locale, null, true)
export default locale
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/src/locale/rw.js | src/locale/rw.js | // Kinyarwanda (Rwanda) [rw]
import dayjs from 'dayjs'
const locale = {
name: 'rw',
weekdays: 'Ku Cyumweru_Kuwa Mbere_Kuwa Kabiri_Kuwa Gatatu_Kuwa Kane_Kuwa Gatanu_Kuwa Gatandatu'.split('_'),
months: 'Mutarama_Gashyantare_Werurwe_Mata_Gicurasi_Kamena_Nyakanga_Kanama_Nzeri_Ukwakira_Ugushyingo_Ukuboza'.split('_'),
relativeTime: {
future: 'mu %s',
past: '%s',
s: 'amasegonda',
m: 'Umunota',
mm: '%d iminota',
h: 'isaha',
hh: '%d amasaha',
d: 'Umunsi',
dd: '%d iminsi',
M: 'ukwezi',
MM: '%d amezi',
y: 'umwaka',
yy: '%d imyaka'
},
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'
},
ordinal: n => n
}
dayjs.locale(locale, null, true)
export default locale
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/src/locale/af.js | src/locale/af.js | // Afrikaans [af]
import dayjs from 'dayjs'
const locale = {
name: 'af',
weekdays: 'Sondag_Maandag_Dinsdag_Woensdag_Donderdag_Vrydag_Saterdag'.split('_'),
months: 'Januarie_Februarie_Maart_April_Mei_Junie_Julie_Augustus_September_Oktober_November_Desember'.split('_'),
weekStart: 1,
weekdaysShort: 'Son_Maa_Din_Woe_Don_Vry_Sat'.split('_'),
monthsShort: 'Jan_Feb_Mrt_Apr_Mei_Jun_Jul_Aug_Sep_Okt_Nov_Des'.split('_'),
weekdaysMin: 'So_Ma_Di_Wo_Do_Vr_Sa'.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: 'oor %s',
past: '%s gelede',
s: "'n paar sekondes",
m: "'n minuut",
mm: '%d minute',
h: "'n uur",
hh: '%d ure',
d: "'n dag",
dd: '%d dae',
M: "'n maand",
MM: '%d maande',
y: "'n jaar",
yy: '%d jaar'
}
}
dayjs.locale(locale, null, true)
export default locale
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/src/locale/ar-sa.js | src/locale/ar-sa.js | // Arabic (Saudi Arabia) [ar-sa]
import dayjs from 'dayjs'
const locale = {
name: 'ar-sa',
weekdays: 'الأحد_الإثنين_الثلاثاء_الأربعاء_الخميس_الجمعة_السبت'.split('_'),
months: 'يناير_فبراير_مارس_أبريل_مايو_يونيو_يوليو_أغسطس_سبتمبر_أكتوبر_نوفمبر_ديسمبر'.split('_'),
weekdaysShort: 'أحد_إثنين_ثلاثاء_أربعاء_خميس_جمعة_سبت'.split('_'),
monthsShort: 'يناير_فبراير_مارس_أبريل_مايو_يونيو_يوليو_أغسطس_سبتمبر_أكتوبر_نوفمبر_ديسمبر'.split('_'),
weekdaysMin: 'ح_ن_ث_ر_خ_ج_س'.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'
},
meridiem: hour => (hour > 12 ? 'م' : 'ص'),
relativeTime: {
future: 'في %s',
past: 'منذ %s',
s: 'ثوان',
m: 'دقيقة',
mm: '%d دقائق',
h: 'ساعة',
hh: '%d ساعات',
d: 'يوم',
dd: '%d أيام',
M: 'شهر',
MM: '%d أشهر',
y: 'سنة',
yy: '%d سنوات'
}
}
dayjs.locale(locale, null, true)
export default locale
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/src/locale/bn.js | src/locale/bn.js | // Bengali [bn]
import dayjs from 'dayjs'
const symbolMap = {
1: '১',
2: '২',
3: '৩',
4: '৪',
5: '৫',
6: '৬',
7: '৭',
8: '৮',
9: '৯',
0: '০'
}
const numberMap = {
'১': '1',
'২': '2',
'৩': '3',
'৪': '4',
'৫': '5',
'৬': '6',
'৭': '7',
'৮': '8',
'৯': '9',
'০': '0'
}
const locale = {
name: 'bn',
weekdays: 'রবিবার_সোমবার_মঙ্গলবার_বুধবার_বৃহস্পতিবার_শুক্রবার_শনিবার'.split('_'),
months: 'জানুয়ারি_ফেব্রুয়ারি_মার্চ_এপ্রিল_মে_জুন_জুলাই_আগস্ট_সেপ্টেম্বর_অক্টোবর_নভেম্বর_ডিসেম্বর'.split('_'),
weekdaysShort: 'রবি_সোম_মঙ্গল_বুধ_বৃহস্পতি_শুক্র_শনি'.split('_'),
monthsShort: 'জানু_ফেব্রু_মার্চ_এপ্রিল_মে_জুন_জুলাই_আগস্ট_সেপ্ট_অক্টো_নভে_ডিসে'.split('_'),
weekdaysMin: 'রবি_সোম_মঙ্গ_বুধ_বৃহঃ_শুক্র_শনি'.split('_'),
preparse(string) {
return string.replace(/[১২৩৪৫৬৭৮৯০]/g, match => numberMap[match])
},
postformat(string) {
return string.replace(/\d/g, match => symbolMap[match])
},
ordinal: n => n,
formats: {
LT: 'A h:mm সময়',
LTS: 'A h:mm:ss সময়',
L: 'DD/MM/YYYY',
LL: 'D MMMM YYYY',
LLL: 'D MMMM YYYY, A h:mm সময়',
LLLL: 'dddd, D MMMM YYYY, A h:mm সময়'
},
relativeTime: {
future: '%s পরে',
past: '%s আগে',
s: 'কয়েক সেকেন্ড',
m: 'এক মিনিট',
mm: '%d মিনিট',
h: 'এক ঘন্টা',
hh: '%d ঘন্টা',
d: 'এক দিন',
dd: '%d দিন',
M: 'এক মাস',
MM: '%d মাস',
y: 'এক বছর',
yy: '%d বছর'
}
}
dayjs.locale(locale, null, true)
export default locale
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/src/locale/zh.js | src/locale/zh.js | // Chinese [zh]
import dayjs from 'dayjs'
const locale = {
name: 'zh',
weekdays: '星期日_星期一_星期二_星期三_星期四_星期五_星期六'.split('_'),
weekdaysShort: '周日_周一_周二_周三_周四_周五_周六'.split('_'),
weekdaysMin: '日_一_二_三_四_五_六'.split('_'),
months: '一月_二月_三月_四月_五月_六月_七月_八月_九月_十月_十一月_十二月'.split('_'),
monthsShort: '1月_2月_3月_4月_5月_6月_7月_8月_9月_10月_11月_12月'.split('_'),
ordinal: (number, period) => {
switch (period) {
case 'W':
return `${number}周`
default:
return `${number}日`
}
},
weekStart: 1,
yearStart: 4,
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分',
l: 'YYYY/M/D',
ll: 'YYYY年M月D日',
lll: 'YYYY年M月D日 HH:mm',
llll: 'YYYY年M月D日dddd HH:mm'
},
relativeTime: {
future: '%s后',
past: '%s前',
s: '几秒',
m: '1 分钟',
mm: '%d 分钟',
h: '1 小时',
hh: '%d 小时',
d: '1 天',
dd: '%d 天',
M: '1 个月',
MM: '%d 个月',
y: '1 年',
yy: '%d 年'
},
meridiem: (hour, minute) => {
const hm = (hour * 100) + minute
if (hm < 600) {
return '凌晨'
} else if (hm < 900) {
return '早上'
} else if (hm < 1100) {
return '上午'
} else if (hm < 1300) {
return '中午'
} else if (hm < 1800) {
return '下午'
}
return '晚上'
}
}
dayjs.locale(locale, null, true)
export default locale
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/src/locale/vi.js | src/locale/vi.js | // Vietnamese [vi]
import dayjs from 'dayjs'
const locale = {
name: 'vi',
weekdays: 'chủ nhật_thứ hai_thứ ba_thứ tư_thứ năm_thứ sáu_thứ bảy'.split('_'),
months: 'tháng 1_tháng 2_tháng 3_tháng 4_tháng 5_tháng 6_tháng 7_tháng 8_tháng 9_tháng 10_tháng 11_tháng 12'.split('_'),
weekStart: 1,
weekdaysShort: 'CN_T2_T3_T4_T5_T6_T7'.split('_'),
monthsShort: 'Th01_Th02_Th03_Th04_Th05_Th06_Th07_Th08_Th09_Th10_Th11_Th12'.split('_'),
weekdaysMin: 'CN_T2_T3_T4_T5_T6_T7'.split('_'),
ordinal: n => n,
formats: {
LT: 'HH:mm',
LTS: 'HH:mm:ss',
L: 'DD/MM/YYYY',
LL: 'D MMMM [năm] YYYY',
LLL: 'D MMMM [năm] YYYY HH:mm',
LLLL: 'dddd, D MMMM [năm] YYYY HH:mm',
l: 'DD/M/YYYY',
ll: 'D MMM YYYY',
lll: 'D MMM YYYY HH:mm',
llll: 'ddd, D MMM YYYY HH:mm'
},
relativeTime: {
future: '%s tới',
past: '%s trước',
s: 'vài giây',
m: 'một phút',
mm: '%d phút',
h: 'một giờ',
hh: '%d giờ',
d: 'một ngày',
dd: '%d ngày',
M: 'một tháng',
MM: '%d tháng',
y: 'một năm',
yy: '%d năm'
}
}
dayjs.locale(locale, null, true)
export default locale
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/src/locale/ar-ly.js | src/locale/ar-ly.js | // Arabic (Lybia) [ar-ly]
import dayjs from 'dayjs'
const locale = {
name: 'ar-ly',
weekdays: 'الأحد_الإثنين_الثلاثاء_الأربعاء_الخميس_الجمعة_السبت'.split('_'),
months: 'يناير_فبراير_مارس_أبريل_مايو_يونيو_يوليو_أغسطس_سبتمبر_أكتوبر_نوفمبر_ديسمبر'.split('_'),
weekStart: 6,
weekdaysShort: 'أحد_إثنين_ثلاثاء_أربعاء_خميس_جمعة_سبت'.split('_'),
monthsShort: 'يناير_فبراير_مارس_أبريل_مايو_يونيو_يوليو_أغسطس_سبتمبر_أكتوبر_نوفمبر_ديسمبر'.split('_'),
weekdaysMin: 'ح_ن_ث_ر_خ_ج_س'.split('_'),
ordinal: n => n,
meridiem: hour => (hour > 12 ? 'م' : 'ص'),
formats: {
LT: 'HH:mm',
LTS: 'HH:mm:ss',
L: 'D/M/YYYY',
LL: 'D MMMM YYYY',
LLL: 'D MMMM YYYY HH:mm',
LLLL: 'dddd D MMMM YYYY HH:mm'
}
}
dayjs.locale(locale, null, true)
export default locale
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/src/locale/kk.js | src/locale/kk.js | // Kazakh [kk]
import dayjs from 'dayjs'
const locale = {
name: 'kk',
weekdays: 'жексенбі_дүйсенбі_сейсенбі_сәрсенбі_бейсенбі_жұма_сенбі'.split('_'),
weekdaysShort: 'жек_дүй_сей_сәр_бей_жұм_сен'.split('_'),
weekdaysMin: 'жк_дй_сй_ср_бй_жм_сн'.split('_'),
months: 'қаңтар_ақпан_наурыз_сәуір_мамыр_маусым_шілде_тамыз_қыркүйек_қазан_қараша_желтоқсан'.split('_'),
monthsShort: 'қаң_ақп_нау_сәу_мам_мау_шіл_там_қыр_қаз_қар_жел'.split('_'),
weekStart: 1,
relativeTime: {
future: '%s ішінде',
past: '%s бұрын',
s: 'бірнеше секунд',
m: 'бір минут',
mm: '%d минут',
h: 'бір сағат',
hh: '%d сағат',
d: 'бір күн',
dd: '%d күн',
M: 'бір ай',
MM: '%d ай',
y: 'бір жыл',
yy: '%d жыл'
},
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'
}
}
dayjs.locale(locale, null, true)
export default locale
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/src/locale/it.js | src/locale/it.js | // Italian [it]
import dayjs from 'dayjs'
const locale = {
name: 'it',
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('_'),
weekStart: 1,
monthsShort: 'gen_feb_mar_apr_mag_giu_lug_ago_set_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: '%s fa',
s: 'qualche secondo',
m: 'un minuto',
mm: '%d minuti',
h: 'un\'ora',
hh: '%d ore',
d: 'un giorno',
dd: '%d giorni',
M: 'un mese',
MM: '%d mesi',
y: 'un anno',
yy: '%d anni'
},
ordinal: n => `${n}º`
}
dayjs.locale(locale, null, true)
export default locale
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/src/locale/jv.js | src/locale/jv.js | // Javanese [jv]
import dayjs from 'dayjs'
const locale = {
name: 'jv',
weekdays: 'Minggu_Senen_Seloso_Rebu_Kemis_Jemuwah_Septu'.split('_'),
months: 'Januari_Februari_Maret_April_Mei_Juni_Juli_Agustus_September_Oktober_Nopember_Desember'.split('_'),
weekStart: 1,
weekdaysShort: 'Min_Sen_Sel_Reb_Kem_Jem_Sep'.split('_'),
monthsShort: 'Jan_Feb_Mar_Apr_Mei_Jun_Jul_Ags_Sep_Okt_Nop_Des'.split('_'),
weekdaysMin: 'Mg_Sn_Sl_Rb_Km_Jm_Sp'.split('_'),
ordinal: n => n,
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: 'wonten ing %s',
past: '%s ingkang kepengker',
s: 'sawetawis detik',
m: 'setunggal menit',
mm: '%d menit',
h: 'setunggal jam',
hh: '%d jam',
d: 'sedinten',
dd: '%d dinten',
M: 'sewulan',
MM: '%d wulan',
y: 'setaun',
yy: '%d taun'
}
}
dayjs.locale(locale, null, true)
export default locale
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/src/locale/az.js | src/locale/az.js | // Azerbaijani [az]
import dayjs from 'dayjs'
const locale = {
name: 'az',
weekdays: 'Bazar_Bazar ertəsi_Çərşənbə axşamı_Çərşənbə_Cümə axşamı_Cümə_Şənbə'.split('_'),
weekdaysShort: 'Baz_BzE_ÇAx_Çər_CAx_Cüm_Şən'.split('_'),
weekdaysMin: 'Bz_BE_ÇA_Çə_CA_Cü_Şə'.split('_'),
months: 'yanvar_fevral_mart_aprel_may_iyun_iyul_avqust_sentyabr_oktyabr_noyabr_dekabr'.split('_'),
monthsShort: 'yan_fev_mar_apr_may_iyn_iyl_avq_sen_okt_noy_dek'.split('_'),
weekStart: 1,
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 sonra',
past: '%s əvvəl',
s: 'bir neçə saniyə',
m: 'bir dəqiqə',
mm: '%d dəqiqə',
h: 'bir saat',
hh: '%d saat',
d: 'bir gün',
dd: '%d gün',
M: 'bir ay',
MM: '%d ay',
y: 'bir il',
yy: '%d il'
},
ordinal: n => n
}
dayjs.locale(locale, null, true)
export default locale
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/src/locale/mk.js | src/locale/mk.js | // Macedonian [mk]
import dayjs from 'dayjs'
const locale = {
name: 'mk',
weekdays: 'недела_понеделник_вторник_среда_четврток_петок_сабота'.split('_'),
months: 'јануари_февруари_март_април_мај_јуни_јули_август_септември_октомври_ноември_декември'.split('_'),
weekStart: 1,
weekdaysShort: 'нед_пон_вто_сре_чет_пет_саб'.split('_'),
monthsShort: 'јан_фев_мар_апр_мај_јун_јул_авг_сеп_окт_ное_дек'.split('_'),
weekdaysMin: 'нe_пo_вт_ср_че_пе_сa'.split('_'),
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'
},
relativeTime: {
future: 'после %s',
past: 'пред %s',
s: 'неколку секунди',
m: 'минута',
mm: '%d минути',
h: 'час',
hh: '%d часа',
d: 'ден',
dd: '%d дена',
M: 'месец',
MM: '%d месеци',
y: 'година',
yy: '%d години'
}
}
dayjs.locale(locale, null, true)
export default locale
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/src/locale/sv.js | src/locale/sv.js | // Swedish [sv]
import dayjs from 'dayjs'
const locale = {
name: 'sv',
weekdays: 'söndag_måndag_tisdag_onsdag_torsdag_fredag_lördag'.split('_'),
weekdaysShort: 'sön_mån_tis_ons_tor_fre_lör'.split('_'),
weekdaysMin: 'sö_må_ti_on_to_fr_lö'.split('_'),
months: 'januari_februari_mars_april_maj_juni_juli_augusti_september_oktober_november_december'.split('_'),
monthsShort: 'jan_feb_mar_apr_maj_jun_jul_aug_sep_okt_nov_dec'.split('_'),
weekStart: 1,
yearStart: 4,
ordinal: (n) => {
const b = n % 10
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',
lll: 'D MMM YYYY HH:mm',
llll: 'ddd D MMM YYYY HH:mm'
},
relativeTime: {
future: 'om %s',
past: 'för %s sedan',
s: 'några sekunder',
m: 'en minut',
mm: '%d minuter',
h: 'en timme',
hh: '%d timmar',
d: 'en dag',
dd: '%d dagar',
M: 'en månad',
MM: '%d månader',
y: 'ett år',
yy: '%d år'
}
}
dayjs.locale(locale, null, true)
export default locale
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/src/locale/ca.js | src/locale/ca.js | // Catalan [ca]
import dayjs from 'dayjs'
const locale = {
name: 'ca',
weekdays: 'Diumenge_Dilluns_Dimarts_Dimecres_Dijous_Divendres_Dissabte'.split('_'),
weekdaysShort: 'Dg._Dl._Dt._Dc._Dj._Dv._Ds.'.split('_'),
weekdaysMin: 'Dg_Dl_Dt_Dc_Dj_Dv_Ds'.split('_'),
months: 'Gener_Febrer_Març_Abril_Maig_Juny_Juliol_Agost_Setembre_Octubre_Novembre_Desembre'.split('_'),
monthsShort: 'Gen._Febr._Març_Abr._Maig_Juny_Jul._Ag._Set._Oct._Nov._Des.'.split('_'),
weekStart: 1,
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',
ll: 'D MMM YYYY',
lll: 'D MMM YYYY, H:mm',
llll: 'ddd D MMM YYYY, H:mm'
},
relativeTime: {
future: 'd\'aquí %s',
past: 'fa %s',
s: 'uns segons',
m: 'un minut',
mm: '%d minuts',
h: 'una hora',
hh: '%d hores',
d: 'un dia',
dd: '%d dies',
M: 'un mes',
MM: '%d mesos',
y: 'un any',
yy: '%d anys'
},
ordinal: (n) => {
let ord
if (n === 1 || n === 3) ord = 'r'
else if (n === 2) ord = 'n'
else if (n === 4) ord = 't'
else ord = 'è'
return `${n}${ord}`
}
}
dayjs.locale(locale, null, true)
export default locale
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/src/locale/ka.js | src/locale/ka.js | // Georgian [ka]
import dayjs from 'dayjs'
const locale = {
name: 'ka',
weekdays: 'კვირა_ორშაბათი_სამშაბათი_ოთხშაბათი_ხუთშაბათი_პარასკევი_შაბათი'.split('_'),
weekdaysShort: 'კვი_ორშ_სამ_ოთხ_ხუთ_პარ_შაბ'.split('_'),
weekdaysMin: 'კვ_ორ_სა_ოთ_ხუ_პა_შა'.split('_'),
months: 'იანვარი_თებერვალი_მარტი_აპრილი_მაისი_ივნისი_ივლისი_აგვისტო_სექტემბერი_ოქტომბერი_ნოემბერი_დეკემბერი'.split('_'),
monthsShort: 'იან_თებ_მარ_აპრ_მაი_ივნ_ივლ_აგვ_სექ_ოქტ_ნოე_დეკ'.split('_'),
weekStart: 1,
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 წინ',
s: 'წამი',
m: 'წუთი',
mm: '%d წუთი',
h: 'საათი',
hh: '%d საათის',
d: 'დღეს',
dd: '%d დღის განმავლობაში',
M: 'თვის',
MM: '%d თვის',
y: 'წელი',
yy: '%d წლის'
},
ordinal: n => n
}
dayjs.locale(locale, null, true)
export default locale
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/src/locale/en-au.js | src/locale/en-au.js | // English (Australia) [en-au]
import dayjs from 'dayjs'
const locale = {
name: 'en-au',
weekdays: 'Sunday_Monday_Tuesday_Wednesday_Thursday_Friday_Saturday'.split('_'),
months: 'January_February_March_April_May_June_July_August_September_October_November_December'.split('_'),
weekStart: 1,
weekdaysShort: 'Sun_Mon_Tue_Wed_Thu_Fri_Sat'.split('_'),
monthsShort: 'Jan_Feb_Mar_Apr_May_Jun_Jul_Aug_Sep_Oct_Nov_Dec'.split('_'),
weekdaysMin: 'Su_Mo_Tu_We_Th_Fr_Sa'.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: 'in %s',
past: '%s ago',
s: 'a few seconds',
m: 'a minute',
mm: '%d minutes',
h: 'an hour',
hh: '%d hours',
d: 'a day',
dd: '%d days',
M: 'a month',
MM: '%d months',
y: 'a year',
yy: '%d years'
},
ordinal: (n) => {
const s = ['th', 'st', 'nd', 'rd']
const v = n % 100
return `[${n}${(s[(v - 20) % 10] || s[v] || s[0])}]`
}
}
dayjs.locale(locale, null, true)
export default locale
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/src/locale/oc-lnc.js | src/locale/oc-lnc.js | // Occitan, lengadocian dialecte [oc-lnc]
import dayjs from 'dayjs'
const locale = {
name: 'oc-lnc',
weekdays: 'dimenge_diluns_dimars_dimècres_dijòus_divendres_dissabte'.split('_'),
weekdaysShort: 'Dg_Dl_Dm_Dc_Dj_Dv_Ds'.split('_'),
weekdaysMin: 'dg_dl_dm_dc_dj_dv_ds'.split('_'),
months: 'genièr_febrièr_març_abrial_mai_junh_julhet_agost_setembre_octòbre_novembre_decembre'.split('_'),
monthsShort: 'gen_feb_març_abr_mai_junh_julh_ago_set_oct_nov_dec'.split('_'),
weekStart: 1,
formats: {
LT: 'H:mm',
LTS: 'H:mm:ss',
L: 'DD/MM/YYYY',
LL: 'D MMMM [de] YYYY',
LLL: 'D MMMM [de] YYYY [a] H:mm',
LLLL: 'dddd D MMMM [de] YYYY [a] H:mm'
},
relativeTime: {
future: 'd\'aquí %s',
past: 'fa %s',
s: 'unas segondas',
m: 'una minuta',
mm: '%d minutas',
h: 'una ora',
hh: '%d oras',
d: 'un jorn',
dd: '%d jorns',
M: 'un mes',
MM: '%d meses',
y: 'un an',
yy: '%d ans'
},
ordinal: n => `${n}º`
}
dayjs.locale(locale, null, true)
export default locale
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/src/locale/ss.js | src/locale/ss.js | // siSwati [ss]
import dayjs from 'dayjs'
const locale = {
name: 'ss',
weekdays: 'Lisontfo_Umsombuluko_Lesibili_Lesitsatfu_Lesine_Lesihlanu_Umgcibelo'.split('_'),
months: "Bhimbidvwane_Indlovana_Indlov'lenkhulu_Mabasa_Inkhwekhweti_Inhlaba_Kholwane_Ingci_Inyoni_Imphala_Lweti_Ingongoni".split('_'),
weekStart: 1,
weekdaysShort: 'Lis_Umb_Lsb_Les_Lsi_Lsh_Umg'.split('_'),
monthsShort: 'Bhi_Ina_Inu_Mab_Ink_Inh_Kho_Igc_Iny_Imp_Lwe_Igo'.split('_'),
weekdaysMin: 'Li_Us_Lb_Lt_Ls_Lh_Ug'.split('_'),
ordinal: n => n,
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: 'nga %s',
past: 'wenteka nga %s',
s: 'emizuzwana lomcane',
m: 'umzuzu',
mm: '%d emizuzu',
h: 'lihora',
hh: '%d emahora',
d: 'lilanga',
dd: '%d emalanga',
M: 'inyanga',
MM: '%d tinyanga',
y: 'umnyaka',
yy: '%d iminyaka'
}
}
dayjs.locale(locale, null, true)
export default locale
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/src/locale/uk.js | src/locale/uk.js | // Ukrainian [uk]
import dayjs from 'dayjs'
const monthFormat = 'січня_лютого_березня_квітня_травня_червня_липня_серпня_вересня_жовтня_листопада_грудня'.split('_')
const monthStandalone = 'січень_лютий_березень_квітень_травень_червень_липень_серпень_вересень_жовтень_листопад_грудень'.split('_')
const MONTHS_IN_FORMAT = /D[oD]?(\[[^[\]]*\]|\s)+MMMM?/
function plural(word, num) {
const forms = word.split('_')
return num % 10 === 1 && num % 100 !== 11 ? forms[0] : (num % 10 >= 2 && num % 10 <= 4 && (num % 100 < 10 || num % 100 >= 20) ? forms[1] : forms[2]) // eslint-disable-line
}
function relativeTimeWithPlural(number, withoutSuffix, key) {
const format = {
ss: withoutSuffix ? 'секунда_секунди_секунд' : 'секунду_секунди_секунд',
mm: withoutSuffix ? 'хвилина_хвилини_хвилин' : 'хвилину_хвилини_хвилин',
hh: withoutSuffix ? 'година_години_годин' : 'годину_години_годин',
dd: 'день_дні_днів',
MM: 'місяць_місяці_місяців',
yy: 'рік_роки_років'
}
if (key === 'm') {
return withoutSuffix ? 'хвилина' : 'хвилину'
} else if (key === 'h') {
return withoutSuffix ? 'година' : 'годину'
}
return `${number} ${plural(format[key], +number)}`
}
const months = (dayjsInstance, format) => {
if (MONTHS_IN_FORMAT.test(format)) {
return monthFormat[dayjsInstance.month()]
}
return monthStandalone[dayjsInstance.month()]
}
months.s = monthStandalone
months.f = monthFormat
const locale = {
name: 'uk',
weekdays: 'неділя_понеділок_вівторок_середа_четвер_п’ятниця_субота'.split('_'),
weekdaysShort: 'ндл_пнд_втр_срд_чтв_птн_сбт'.split('_'),
weekdaysMin: 'нд_пн_вт_ср_чт_пт_сб'.split('_'),
months,
monthsShort: 'січ_лют_бер_квіт_трав_черв_лип_серп_вер_жовт_лист_груд'.split('_'),
weekStart: 1,
relativeTime: {
future: 'за %s',
past: '%s тому',
s: 'декілька секунд',
m: relativeTimeWithPlural,
mm: relativeTimeWithPlural,
h: relativeTimeWithPlural,
hh: relativeTimeWithPlural,
d: 'день',
dd: relativeTimeWithPlural,
M: 'місяць',
MM: relativeTimeWithPlural,
y: 'рік',
yy: relativeTimeWithPlural
},
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'
}
}
dayjs.locale(locale, null, true)
export default locale
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/src/locale/ar-ma.js | src/locale/ar-ma.js | // Arabic (Morocco) [ar-ma]
import dayjs from 'dayjs'
const locale = {
name: 'ar-ma',
weekdays: 'الأحد_الإثنين_الثلاثاء_الأربعاء_الخميس_الجمعة_السبت'.split('_'),
months: 'يناير_فبراير_مارس_أبريل_ماي_يونيو_يوليوز_غشت_شتنبر_أكتوبر_نونبر_دجنبر'.split('_'),
weekStart: 6,
weekdaysShort: 'احد_إثنين_ثلاثاء_اربعاء_خميس_جمعة_سبت'.split('_'),
monthsShort: 'يناير_فبراير_مارس_أبريل_ماي_يونيو_يوليوز_غشت_شتنبر_أكتوبر_نونبر_دجنبر'.split('_'),
weekdaysMin: 'ح_ن_ث_ر_خ_ج_س'.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'
},
meridiem: hour => (hour > 12 ? 'م' : 'ص'),
relativeTime: {
future: 'في %s',
past: 'منذ %s',
s: 'ثوان',
m: 'دقيقة',
mm: '%d دقائق',
h: 'ساعة',
hh: '%d ساعات',
d: 'يوم',
dd: '%d أيام',
M: 'شهر',
MM: '%d أشهر',
y: 'سنة',
yy: '%d سنوات'
}
}
dayjs.locale(locale, null, true)
export default locale
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/src/locale/gl.js | src/locale/gl.js | // Galician [gl]
import dayjs from 'dayjs'
const locale = {
name: 'gl',
weekdays: 'domingo_luns_martes_mércores_xoves_venres_sábado'.split('_'),
months: 'xaneiro_febreiro_marzo_abril_maio_xuño_xullo_agosto_setembro_outubro_novembro_decembro'.split('_'),
weekStart: 1,
weekdaysShort: 'dom._lun._mar._mér._xov._ven._sáb.'.split('_'),
monthsShort: 'xan._feb._mar._abr._mai._xuñ._xul._ago._set._out._nov._dec.'.split('_'),
weekdaysMin: 'do_lu_ma_mé_xo_ve_sá'.split('_'),
ordinal: n => `${n}º`,
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: 'fai %s',
s: 'uns segundos',
m: 'un minuto',
mm: '%d minutos',
h: 'unha hora',
hh: '%d horas',
d: 'un día',
dd: '%d días',
M: 'un mes',
MM: '%d meses',
y: 'un ano',
yy: '%d anos'
}
}
dayjs.locale(locale, null, true)
export default locale
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/src/locale/mn.js | src/locale/mn.js | // Mongolian [mn]
import dayjs from 'dayjs'
const locale = {
name: 'mn',
weekdays: 'Ням_Даваа_Мягмар_Лхагва_Пүрэв_Баасан_Бямба'.split('_'),
months: 'Нэгдүгээр сар_Хоёрдугаар сар_Гуравдугаар сар_Дөрөвдүгээр сар_Тавдугаар сар_Зургадугаар сар_Долдугаар сар_Наймдугаар сар_Есдүгээр сар_Аравдугаар сар_Арван нэгдүгээр сар_Арван хоёрдугаар сар'.split('_'),
weekdaysShort: 'Ням_Дав_Мяг_Лха_Пүр_Баа_Бям'.split('_'),
monthsShort: '1 сар_2 сар_3 сар_4 сар_5 сар_6 сар_7 сар_8 сар_9 сар_10 сар_11 сар_12 сар'.split('_'),
weekdaysMin: 'Ня_Да_Мя_Лх_Пү_Ба_Бя'.split('_'),
ordinal: n => n,
formats: {
LT: 'HH:mm',
LTS: 'HH:mm:ss',
L: 'YYYY-MM-DD',
LL: 'YYYY оны MMMMын D',
LLL: 'YYYY оны MMMMын D HH:mm',
LLLL: 'dddd, YYYY оны MMMMын D HH:mm'
},
relativeTime: {
future: '%s',
past: '%s',
s: 'саяхан',
m: 'м',
mm: '%dм',
h: '1ц',
hh: '%dц',
d: '1ө',
dd: '%dө',
M: '1с',
MM: '%dс',
y: '1ж',
yy: '%dж'
}
}
dayjs.locale(locale, null, true)
export default locale
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/src/locale/uz.js | src/locale/uz.js | // Uzbek [uz]
import dayjs from 'dayjs'
const locale = {
name: 'uz',
weekdays: 'Якшанба_Душанба_Сешанба_Чоршанба_Пайшанба_Жума_Шанба'.split('_'),
months: 'январ_феврал_март_апрел_май_июн_июл_август_сентябр_октябр_ноябр_декабр'.split('_'),
weekStart: 1,
weekdaysShort: 'Якш_Душ_Сеш_Чор_Пай_Жум_Шан'.split('_'),
monthsShort: 'янв_фев_мар_апр_май_июн_июл_авг_сен_окт_ноя_дек'.split('_'),
weekdaysMin: 'Як_Ду_Се_Чо_Па_Жу_Ша'.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: 'D MMMM YYYY, dddd HH:mm'
},
relativeTime: {
future: 'Якин %s ичида',
past: '%s олдин',
s: 'фурсат',
m: 'бир дакика',
mm: '%d дакика',
h: 'бир соат',
hh: '%d соат',
d: 'бир кун',
dd: '%d кун',
M: 'бир ой',
MM: '%d ой',
y: 'бир йил',
yy: '%d йил'
}
}
dayjs.locale(locale, null, true)
export default locale
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/src/locale/en-ca.js | src/locale/en-ca.js | // English (Canada) [en-ca]
import dayjs from 'dayjs'
const locale = {
name: 'en-ca',
weekdays: 'Sunday_Monday_Tuesday_Wednesday_Thursday_Friday_Saturday'.split('_'),
months: 'January_February_March_April_May_June_July_August_September_October_November_December'.split('_'),
weekdaysShort: 'Sun_Mon_Tue_Wed_Thu_Fri_Sat'.split('_'),
monthsShort: 'Jan_Feb_Mar_Apr_May_Jun_Jul_Aug_Sep_Oct_Nov_Dec'.split('_'),
weekdaysMin: 'Su_Mo_Tu_We_Th_Fr_Sa'.split('_'),
ordinal: n => n,
formats: {
LT: 'h:mm A',
LTS: 'h:mm:ss A',
L: 'YYYY-MM-DD',
LL: 'MMMM D, YYYY',
LLL: 'MMMM D, YYYY h:mm A',
LLLL: 'dddd, MMMM D, YYYY h:mm A'
},
relativeTime: {
future: 'in %s',
past: '%s ago',
s: 'a few seconds',
m: 'a minute',
mm: '%d minutes',
h: 'an hour',
hh: '%d hours',
d: 'a day',
dd: '%d days',
M: 'a month',
MM: '%d months',
y: 'a year',
yy: '%d years'
}
}
dayjs.locale(locale, null, true)
export default locale
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/src/locale/ja.js | src/locale/ja.js | // Japanese [ja]
import dayjs from 'dayjs'
const locale = {
name: 'ja',
weekdays: '日曜日_月曜日_火曜日_水曜日_木曜日_金曜日_土曜日'.split('_'),
weekdaysShort: '日_月_火_水_木_金_土'.split('_'),
weekdaysMin: '日_月_火_水_木_金_土'.split('_'),
months: '1月_2月_3月_4月_5月_6月_7月_8月_9月_10月_11月_12月'.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',
l: 'YYYY/MM/DD',
ll: 'YYYY年M月D日',
lll: 'YYYY年M月D日 HH:mm',
llll: 'YYYY年M月D日(ddd) HH:mm'
},
meridiem: hour => (hour < 12 ? '午前' : '午後'),
relativeTime: {
future: '%s後',
past: '%s前',
s: '数秒',
m: '1分',
mm: '%d分',
h: '1時間',
hh: '%d時間',
d: '1日',
dd: '%d日',
M: '1ヶ月',
MM: '%dヶ月',
y: '1年',
yy: '%d年'
}
}
dayjs.locale(locale, null, true)
export default locale
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/src/locale/uz-latn.js | src/locale/uz-latn.js | // Uzbek Latin [uz-latn]
import dayjs from 'dayjs'
const locale = {
name: 'uz-latn',
weekdays: 'Yakshanba_Dushanba_Seshanba_Chorshanba_Payshanba_Juma_Shanba'.split('_'),
months: 'Yanvar_Fevral_Mart_Aprel_May_Iyun_Iyul_Avgust_Sentabr_Oktabr_Noyabr_Dekabr'.split('_'),
weekStart: 1,
weekdaysShort: 'Yak_Dush_Sesh_Chor_Pay_Jum_Shan'.split('_'),
monthsShort: 'Yan_Fev_Mar_Apr_May_Iyun_Iyul_Avg_Sen_Okt_Noy_Dek'.split('_'),
weekdaysMin: 'Ya_Du_Se_Cho_Pa_Ju_Sha'.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: 'D MMMM YYYY, dddd HH:mm'
},
relativeTime: {
future: 'Yaqin %s ichida',
past: '%s oldin',
s: 'soniya',
m: 'bir daqiqa',
mm: '%d daqiqa',
h: 'bir soat',
hh: '%d soat',
d: 'bir kun',
dd: '%d kun',
M: 'bir oy',
MM: '%d oy',
y: 'bir yil',
yy: '%d yil'
}
}
dayjs.locale(locale, null, true)
export default locale
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/src/locale/ku.js | src/locale/ku.js | // Kurdish [ku]
import dayjs from 'dayjs'
export const englishToArabicNumbersMap = {
1: '١',
2: '٢',
3: '٣',
4: '٤',
5: '٥',
6: '٦',
7: '٧',
8: '٨',
9: '٩',
0: '٠'
}
const arabicToEnglishNumbersMap = {
'١': '1',
'٢': '2',
'٣': '3',
'٤': '4',
'٥': '5',
'٦': '6',
'٧': '7',
'٨': '8',
'٩': '9',
'٠': '0'
}
const months = [
'کانوونی دووەم',
'شوبات',
'ئادار',
'نیسان',
'ئایار',
'حوزەیران',
'تەممووز',
'ئاب',
'ئەیلوول',
'تشرینی یەکەم',
'تشرینی دووەم',
'کانوونی یەکەم'
]
const locale = {
name: 'ku',
months,
monthsShort: months,
weekdays: 'یەکشەممە_دووشەممە_سێشەممە_چوارشەممە_پێنجشەممە_هەینی_شەممە'.split('_'),
weekdaysShort: 'یەکشەم_دووشەم_سێشەم_چوارشەم_پێنجشەم_هەینی_شەممە'.split('_'),
weekStart: 6,
weekdaysMin: 'ی_د_س_چ_پ_هـ_ش'.split('_'),
preparse(string) {
return string
.replace(/[١٢٣٤٥٦٧٨٩٠]/g, match => arabicToEnglishNumbersMap[match])
.replace(/،/g, ',')
},
postformat(string) {
return string
.replace(/\d/g, match => englishToArabicNumbersMap[match])
.replace(/,/g, '،')
},
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'
},
meridiem: hour => (hour < 12 ? 'پ.ن' : 'د.ن'),
relativeTime: {
future: 'لە %s',
past: 'لەمەوپێش %s',
s: 'چەند چرکەیەک',
m: 'یەک خولەک',
mm: '%d خولەک',
h: 'یەک کاتژمێر',
hh: '%d کاتژمێر',
d: 'یەک ڕۆژ',
dd: '%d ڕۆژ',
M: 'یەک مانگ',
MM: '%d مانگ',
y: 'یەک ساڵ',
yy: '%d ساڵ'
}
}
dayjs.locale(locale, null, true)
export default locale
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/test/timezone.test.js | test/timezone.test.js | import MockDate from 'mockdate'
import moment from 'moment'
import dayjs from '../src'
import timezone from '../src/plugin/timezone'
import utc from '../src/plugin/utc'
dayjs.extend(utc)
dayjs.extend(timezone)
beforeEach(() => {
MockDate.set(new Date())
})
afterEach(() => {
MockDate.reset()
})
it('Add Time days (DST)', () => {
// change timezone before running test
// New Zealand (-720)
expect(dayjs('2018-04-01').add(1, 'd').format()).toBe(moment('2018-04-01').add(1, 'd').format())
expect(dayjs('2018-03-28').add(1, 'w').format()).toBe(moment('2018-03-28').add(1, 'w').format())
// London (-60)
expect(dayjs('2018-10-28').add(1, 'd').format()).toBe(moment('2018-10-28').add(1, 'd').format())
expect(dayjs('2018-10-26').add(1, 'w').format()).toBe(moment('2018-10-26').add(1, 'w').format())
})
it('Utc Offset', () => {
expect(dayjs().utcOffset()).toBe(moment().utcOffset())
})
it('Diff (DST)', () => {
const day = '2018-10-28'
const dayjsA = dayjs(day)
const dayjsB = dayjs(day).add(-1000, 'days')
const momentA = moment(day)
const momentB = moment(day).add(-1000, 'days')
const units = ['seconds', 'minutes', 'hours', 'days', 'weeks', 'months', 'quarters', 'years']
units.forEach((unit) => {
expect(dayjsA.diff(dayjsB, unit)).toBe(momentA.diff(momentB, unit))
expect(dayjsA.diff(dayjsB, unit, true)).toBe(momentA.diff(momentB, unit, true))
})
})
it('UTC add day in DST', () => {
const testDate = '2019-03-10'
const dayTest = dayjs(testDate)
.utc()
.startOf('day')
const momentTest = moment(testDate)
.utc()
.startOf('day')
expect(dayTest.add(1, 'day').format())
.toBe(momentTest.clone().add(1, 'day').format())
expect(dayTest.add(2, 'day').format())
.toBe(momentTest.clone().add(2, 'day').format())
})
it('UTC and utcOffset', () => {
const test1 = 1331449199000 // 2012/3/11 06:59:59 GMT+0000
expect(dayjs(test1).utcOffset(-300).format())
.toBe(moment(test1).utcOffset(-300).format())
const test2 = '2000-01-01T06:31:00Z'
expect(dayjs.utc(test2).utcOffset(-60).format())
.toBe(moment.utc(test2).utcOffset(-60).format())
// across DST, copied from utc.test.js#get utc offset with a number value
const time = '2021-02-28 19:40:10'
const hoursOffset = -8
const daysJS = dayjs(time).utc().utcOffset(hoursOffset * 60, true)
const momentJS = moment(time).utc(true).utcOffset(hoursOffset, true)
expect(daysJS.toISOString()).toEqual(momentJS.toISOString())
})
it('UTC diff in DST', () => {
// DST till 2020-10-25
const day1 = dayjs.utc('20201023') // in DST
const day2 = dayjs.utc('20201026')
expect(day1.diff(day2, 'd'))
.toBe(-3)
})
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/test/manipulate.test.js | test/manipulate.test.js | import moment from 'moment'
import MockDate from 'mockdate'
import dayjs from '../src'
import '../src/locale/zh-cn'
import '../src/locale/ar'
beforeEach(() => {
MockDate.set(new Date())
})
afterEach(() => {
MockDate.reset()
})
describe('StartOf EndOf', () => {
it('StartOf EndOf Year ... with s and upper case', () => {
const testArr = ['Year', 'year', 'YearS', 'month', 'day', 'date',
'week', 'hour', 'minute', 'second']
testArr.forEach((d) => {
expect(dayjs().startOf(d).valueOf()).toBe(moment().startOf(d).valueOf())
expect(dayjs().endOf(d).valueOf()).toBe(moment().endOf(d).valueOf())
})
})
it('StartOf EndOf Other -> no change', () => {
expect(dayjs().startOf('otherString').valueOf()).toBe(moment().startOf('otherString').valueOf())
expect(dayjs().endOf('otherString').valueOf()).toBe(moment().endOf('otherString').valueOf())
})
it('StartOf week with locale', () => {
const testDate = [undefined, '2019-02-10', '2019-02-11', '2019-02-12', '2019-02-13', '2019-02-14', '2019-02-15', '2019-02-16']
const testLocale = ['zh-cn', 'ar', 'en']
testDate.forEach((d) => {
testLocale.forEach((l) => {
expect(dayjs(d).locale(l).startOf('week').date())
.toBe(moment(d).locale(l).startOf('week').date())
expect(dayjs(d).locale(l).endOf('week').date())
.toBe(moment(d).locale(l).endOf('week').date())
})
})
})
})
it('Add Time days', () => {
expect(dayjs().add(1, 'ms').valueOf()).toBe(moment().add(1, 'ms').valueOf())
expect(dayjs().add(1, 'milliseconds').valueOf()).toBe(moment().add(1, 'milliseconds').valueOf())
expect(dayjs().add(1, 's').valueOf()).toBe(moment().add(1, 's').valueOf())
expect(dayjs().add(1, 'seconds').valueOf()).toBe(moment().add(1, 'seconds').valueOf())
expect(dayjs().add(1, 'm').valueOf()).toBe(moment().add(1, 'm').valueOf())
expect(dayjs().add(1, 'minutes').valueOf()).toBe(moment().add(1, 'minutes').valueOf())
expect(dayjs().add(1, 'h').valueOf()).toBe(moment().add(1, 'h').valueOf())
expect(dayjs().add(1, 'hours').valueOf()).toBe(moment().add(1, 'hours').valueOf())
expect(dayjs().add(1, 'w').valueOf()).toBe(moment().add(1, 'w').valueOf())
expect(dayjs().add(1, 'weeks').valueOf()).toBe(moment().add(1, 'weeks').valueOf())
expect(dayjs().add(1, 'd').valueOf()).toBe(moment().add(1, 'd').valueOf())
expect(dayjs().add(1, 'days').valueOf()).toBe(moment().add(1, 'days').valueOf())
expect(dayjs().add(1, 'M').valueOf()).toBe(moment().add(1, 'M').valueOf())
expect(dayjs().add(1, 'y').valueOf()).toBe(moment().add(1, 'y').valueOf())
expect(dayjs('20111031').add(1, 'months').valueOf()).toBe(moment('20111031').add(1, 'months').valueOf())
expect(dayjs('20160131').add(1, 'months').valueOf()).toBe(moment('20160131').add(1, 'months').valueOf())
expect(dayjs('20160229').add(1, 'year').valueOf()).toBe(moment('20160229').add(1, 'year').valueOf())
expect(dayjs().add('2', 'years').valueOf()).toBe(moment().add('2', 'years').valueOf())
})
it('Add Time with decimal', () => {
expect(dayjs().add(0.4, 'day').valueOf()).toBe(moment().add(0.4, 'day').valueOf())
expect(dayjs().add(0.5, 'day').valueOf()).toBe(moment().add(0.5, 'day').valueOf())
expect(dayjs().add(0.4, 'week').valueOf()).toBe(moment().add(0.4, 'week').valueOf())
expect(dayjs().add(0.5, 'week').valueOf()).toBe(moment().add(0.5, 'week').valueOf())
})
it('Subtract Time days', () => {
expect(dayjs().subtract(1, 'days').valueOf()).toBe(moment().subtract(1, 'days').valueOf())
})
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/test/utils.test.js | test/utils.test.js | import Utils from '../src/utils'
const prettyUnit = Utils.p
const padStart = Utils.s
const padZoneStr = Utils.z
it('PrettyUnit', () => {
expect(prettyUnit('Days')).toBe('day')
expect(prettyUnit('days')).toBe('day')
expect(prettyUnit('day')).toBe('day')
expect(prettyUnit('quarter')).toBe('quarter')
expect(prettyUnit('quarters')).toBe('quarter')
expect(prettyUnit('D')).toBe('date')
expect(prettyUnit('d')).toBe('day')
expect(prettyUnit('M')).toBe('month')
expect(prettyUnit('y')).toBe('year')
expect(prettyUnit('h')).toBe('hour')
expect(prettyUnit('m')).toBe('minute')
expect(prettyUnit('s')).toBe('second')
expect(prettyUnit('ms')).toBe('millisecond')
expect(prettyUnit('Q')).toBe('quarter')
expect(prettyUnit()).toBe('')
})
it('PadZoneStr', () => {
const instance = {}
instance.utcOffset = () => 0 * -1
expect(padZoneStr(instance)).toBe('+00:00')
instance.utcOffset = () => 1 * 60 * -1
expect(padZoneStr(instance)).toBe('-01:00')
instance.utcOffset = () => -1 * 60 * -1
expect(padZoneStr(instance)).toBe('+01:00')
instance.utcOffset = () => -10 * 60 * -1
expect(padZoneStr(instance)).toBe('+10:00')
instance.utcOffset = () => 10 * 60 * -1
expect(padZoneStr(instance)).toBe('-10:00')
instance.utcOffset = () => ((-5 * 60) - 30) * -1
expect(padZoneStr(instance)).toBe('+05:30')
})
it('PadStart', () => {
expect(padStart(1, 2, '0')).toBe('01')
expect(padStart(0, 2, '0')).toBe('00')
})
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/test/plugin.test.js | test/plugin.test.js | import MockDate from 'mockdate'
import dayjs from '../src'
const testPlugin = (o, c, d) => {
c.prototype.newApi = () => ('hello world')
d.newFunc = () => ('hi world')
}
const testPluginWithConfig = (o, c) => {
c.prototype.newApiWithConfig = () => (`hello world ${o || ''}`)
}
dayjs.extend(testPlugin)
dayjs.extend(testPluginWithConfig, 'good')
beforeEach(() => {
MockDate.set(new Date())
})
afterEach(() => {
MockDate.reset()
})
it('Plugin extend method and option', () => {
expect(dayjs().newApi()).toBe('hello world')
expect(dayjs().newApiWithConfig()).toBe('hello world good')
})
it('Plugin extend dayjs', () => {
expect(dayjs.newFunc()).toBe('hi world')
})
it('Plugin use core utils', () => {
// u => isUndefined
expect(dayjs().$utils().u).toBeInstanceOf(Function)
})
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/test/browser.spec.js | test/browser.spec.js | /* eslint-disable prefer-arrow-callback,func-names */
// Please do NOT modify or change this file
// Checkout our unit test files in test/*.test.js
describe('Install', function () {
it('window.dayjs ', function () {
if (!window.dayjs) throw new Error('No window.dayjs')
})
})
describe('Core APIs', function () {
it('Chain Methods', function () {
expect(dayjs('2011-02-05T14:48:00.000Z')
.clone()
.set('month', 3)
.set('second', 30)
.endOf('month')
.startOf('week')
.add(1, 'day')
.subtract(1, 'year')
.format('{YYYY} MM-DDTHH:mm:ss')).toBe('{2010} 04-25T00:00:00')
})
it('Date parse - nonstandard date string', function () {
expect(dayjs('2018-4-1 1:1:1:22').format('YYYY-MM-DD hh:mm:ss'))
.toBe('2018-04-01 01:01:01')
expect(dayjs('2018-4-1').format('YYYY-MM-DD hh:mm:ss'))
.toBe('2018-04-01 12:00:00')
expect(dayjs('2018-4-1 11:49').format('YYYY-MM-DD hh:mm:ss')) // fix ios bug
.toBe('2018-04-01 11:49:00')
})
})
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/test/parse.test.js | test/parse.test.js | import moment from 'moment'
import MockDate from 'mockdate'
import dayjs from '../src'
import { REGEX_PARSE } from '../src/constant'
beforeEach(() => {
MockDate.set(new Date())
})
afterEach(() => {
MockDate.reset()
})
describe('Parse', () => {
it('Now', () => {
expect(dayjs().valueOf()).toBe(moment().valueOf())
})
it('moment-js like formatted dates', () => {
global.console.warn = jest.genMockFunction()// moment.js '2018-4-1 1:1:1:22' will throw warn
let d = '20130108'
expect(dayjs(d).valueOf()).toBe(moment(d).valueOf())
d = '2018-04-24'
expect(dayjs(d).valueOf()).toBe(moment(d).valueOf())
d = '2018-04-24 11:12'
expect(dayjs(d).format()).toBe(moment(d).format()) // not recommend
d = '2018-05-02 11:12:13'
expect(dayjs(d).valueOf()).toBe(moment(d).valueOf())
d = '2018-05-02 11:12:13.998'
expect(dayjs(d).valueOf()).toBe(moment(d).valueOf())
d = '2018-4-1'
expect(dayjs(d).valueOf()).toBe(moment(d).valueOf()) // not recommend
d = '2018-4-1 11:12'
expect(dayjs(d).format()).toBe(moment(d).format()) // not recommend
d = '2018-4-1 1:1:1:223'
expect(dayjs(d).valueOf()).toBe(moment(d).valueOf()) // not recommend
d = '2018-01'
expect(dayjs(d).valueOf()).toBe(moment(d).valueOf()) // not recommend
d = '2018'
expect(dayjs(d).format()).toBe(moment(d).format()) // not recommend
d = '2018-05-02T11:12:13Z' // should go direct to new Date() rather our regex
expect(dayjs(d).format()).toBe(moment(d).format()) // not recommend
})
it('String ISO 8601 date, time and zone', () => {
const time = '2018-04-04T16:00:00.000Z'
expect(dayjs(time).valueOf()).toBe(moment(time).valueOf())
})
it('String RFC 2822, time and zone', () => {
const time = 'Mon, 11 Feb 2019 09:46:50 GMT+1'
const expected = '2019-02-11T08:46:50.000Z'
const d = dayjs(time)
expect(d.toISOString()).toEqual(expected)
expect(d.valueOf()).toBe(moment(time).valueOf())
})
it('String ECMAScript, time and zone', () => {
// should parse dates formatted in ECMA script format
// see https://www.ecma-international.org/ecma-262/9.0/index.html#sec-date.prototype.tostring
const time = 'Mon Feb 11 2019 11:01:37 GMT+0100 (Mitteleuropäische Normalzeit)'
const expected = '2019-02-11T10:01:37.000Z'
const d = dayjs(time)
expect(d.toISOString()).toEqual(expected)
expect(d.valueOf()).toBe(moment(time).valueOf())
})
it('rejects invalid values', () => {
expect(dayjs({}).isValid()).toBe(false)
expect(dayjs(() => '2018-01-01').isValid()).toBe(false)
expect(dayjs(Infinity).isValid()).toBe(false)
expect(dayjs(NaN).isValid()).toBe(false)
expect(dayjs([2018, 5, 1, 13, 52, 44]).isValid()).toBe(false) // Arrays with time part
})
it('parses Arrays with date part', () => {
const dateParts = [2018, 5, 1]
const expected = '2018-05-01T00:00:00.000Z'
const d = dayjs(dateParts)
const normalized = d.add(d.utcOffset(), 'minutes') // make test run in every timezone
expect(normalized.toISOString()).toEqual(expected)
})
it('parses unlimited millisecond', () => {
const date = '2019-03-25T06:41:00.999999999'
const ds = dayjs(date)
const ms = moment(date)
expect(ds.valueOf()).toEqual(ms.valueOf())
expect(ds.millisecond()).toEqual(ms.millisecond())
})
it('String Other, Undefined and Null and isValid', () => {
global.console.warn = jest.genMockFunction()// moment.js otherString will throw warn
expect(dayjs('otherString').toString().toLowerCase()).toBe(moment('otherString').toString().toLowerCase())
expect(dayjs(undefined).toDate()).toEqual(moment(undefined).toDate())
expect(dayjs().isValid()).toBe(true)
expect(dayjs(undefined).isValid()).toBe(true)
expect(dayjs('').isValid()).toBe(false)
expect(dayjs(null).isValid()).toBe(false)
expect(dayjs('otherString').isValid()).toBe(false)
expect(dayjs(null).toString().toLowerCase()).toBe(moment(null).toString().toLowerCase())
})
})
it('Unix Timestamp Number (milliseconds) 1523520536000', () => {
const timestamp = 1523520536000
expect(dayjs(timestamp).valueOf()).toBe(moment(timestamp).valueOf())
})
it('Unix Timestamp Number (seconds) 1318781876', () => {
const timestamp1 = 1318781876
const timestamp2 = 1318781876.721
expect(dayjs.unix(timestamp1).valueOf()).toBe(moment.unix(timestamp1).valueOf())
expect(dayjs.unix(timestamp2).valueOf()).toBe(moment.unix(timestamp2).valueOf())
})
it('String and Number 20180101', () => {
expect(dayjs(20180101).valueOf()).toBe(moment(20180101).valueOf())
expect(dayjs('20180101').valueOf()).toBe(moment('20180101').valueOf())
})
it('Number 0', () => {
expect(dayjs(0).valueOf()).toBe(moment(0).valueOf())
})
it('Clone not affect each other', () => {
const base = dayjs(20170101)
const year = base.year()
const another = base.set('year', year + 1)
expect(another.unix() - base.unix()).toBe(31536000)
})
it('Clone with same value', () => {
const base = dayjs()
const year = base.year()
const newBase = base.set('year', year + 1)
const another = newBase.clone()
expect(newBase.toString()).toBe(another.toString())
})
describe('REGEX_PARSE', () => {
it('2020/9/30', () => {
const date = '2020/9/30'
const d = date.match(REGEX_PARSE)
expect(dayjs(date).valueOf()).toBe(moment(date).valueOf())
expect(d.join('-')).toBe('2020/9/30-2020-9-30----')
})
it('2019-03-25T06:41:00.999999999', () => {
const date = '2019-03-25T06:41:00.999999999'
const d = date.match(REGEX_PARSE)
expect(dayjs(date).valueOf()).toBe(moment(date).valueOf())
expect(d.join('-')).toBe('2019-03-25T06:41:00.999999999-2019-03-25-06-41-00-999999999')
})
it('20210102T012345', () => {
const date = '20210102T012345'
const d = date.match(REGEX_PARSE)
expect(dayjs(date).valueOf()).toBe(moment(date).valueOf())
expect(d.join('-')).toBe('20210102T012345-2021-01-02-01-23-45-')
})
it('2021-01-02T01:23', () => {
const date = '2021-01-02T01:23'
const d = date.match(REGEX_PARSE)
expect(dayjs(date).valueOf()).toBe(moment(date).valueOf())
expect(d.join('-')).toBe('2021-01-02T01:23-2021-01-02-01-23--')
})
it('2021-01-02T01:23:45', () => {
const date = '2021-01-02T01:23:45'
const d = date.match(REGEX_PARSE)
expect(dayjs(date).valueOf()).toBe(moment(date).valueOf())
expect(d.join('-')).toBe('2021-01-02T01:23:45-2021-01-02-01-23-45-')
})
it('2020-12-31T18:00:00.000-0500 (no regex match)', () => {
const date = '2020-12-31T18:00:00.000-0500'
const d = date.match(REGEX_PARSE)
expect(dayjs(date).valueOf()).toBe(moment(date).valueOf())
expect(d).toBe(null)
})
// format used in timezone plugin utcString
it('2021-1-4 0:42:53:000', () => {
const date = '2021-1-4 0:42:53:000'
const d = date.match(REGEX_PARSE)
expect(dayjs(date).valueOf()).toBe(moment(date).valueOf())
expect(d.join('-')).toBe('2021-1-4 0:42:53:000-2021-1-4-0-42-53-000')
})
it('2020-12-31T18:00:00-05:00 (no regex match)', () => {
const date = '2020-12-31T18:00:00-05:00'
const d = date.match(REGEX_PARSE)
expect(dayjs(date).valueOf()).toBe(moment(date).valueOf())
expect(d).toBe(null)
})
it('2021-01-02T01:23:45-0500 (no regex match)', () => {
const date = '2021-01-02T01:23:45-0500'
const d = date.match(REGEX_PARSE)
expect(dayjs(date).valueOf()).toBe(moment(date).valueOf())
expect(d).toBe(null)
})
it('2021-01-02T01:23:45Z (no regex match)', () => {
const date = '2021-01-02T01:23:45Z'
const d = date.match(REGEX_PARSE)
expect(dayjs(date).valueOf()).toBe(moment(date).valueOf())
expect(d).toBe(null)
})
// dots should not be matched, and fallback to Date
it('2021.01.03', () => {
const date = '2021.01.03'
const d = date.match(REGEX_PARSE)
expect(dayjs(date).valueOf()).toBe(moment(date).valueOf())
expect(d).toBe(null)
})
})
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/test/get-set.test.js | test/get-set.test.js | import moment from 'moment'
import MockDate from 'mockdate'
import dayjs from '../src'
beforeEach(() => {
MockDate.set(new Date())
})
afterEach(() => {
MockDate.reset()
})
it('Year', () => {
expect(dayjs().get('year')).toBe(moment().get('year'))
expect(dayjs().year()).toBe(moment().year())
expect(dayjs().year(0).valueOf()).toBe(moment().year(0).valueOf())
expect(dayjs().year(2000).valueOf()).toBe(moment().year(2000).valueOf())
})
it('Month', () => {
expect(dayjs().get('month')).toBe(moment().get('month'))
expect(dayjs().month()).toBe(moment().month())
expect(dayjs().month(0).valueOf()).toBe(moment().month(0).valueOf())
expect(dayjs().month(1).valueOf()).toBe(moment().month(1).valueOf())
})
it('Day of Week', () => {
expect(dayjs().get('day')).toBe(moment().get('day'))
expect(dayjs().day()).toBe(moment().day())
expect(dayjs().day(0).format()).toBe(moment().day(0).format())
expect(dayjs().day(1).format()).toBe(moment().day(1).format())
})
it('Date', () => {
expect(dayjs().get('date')).toBe(moment().get('date'))
expect(dayjs().date()).toBe(moment().date())
expect(dayjs().date(0).valueOf()).toBe(moment().date(0).valueOf())
expect(dayjs().date(1).valueOf()).toBe(moment().date(1).valueOf())
})
it('Hour', () => {
expect(dayjs().get('hour')).toBe(moment().get('hour'))
expect(dayjs().hour()).toBe(moment().hour())
expect(dayjs().hour(0).valueOf()).toBe(moment().hour(0).valueOf())
expect(dayjs().hour(1).valueOf()).toBe(moment().hour(1).valueOf())
})
it('Minute', () => {
expect(dayjs().get('minute')).toBe(moment().get('minute'))
expect(dayjs().minute()).toBe(moment().minute())
expect(dayjs().minute(0).valueOf()).toBe(moment().minute(0).valueOf())
expect(dayjs().minute(1).valueOf()).toBe(moment().minute(1).valueOf())
})
it('Second', () => {
expect(dayjs().get('second')).toBe(moment().get('second'))
expect(dayjs().second()).toBe(moment().second())
expect(dayjs().second(0).valueOf()).toBe(moment().second(0).valueOf())
expect(dayjs().second(1).valueOf()).toBe(moment().second(1).valueOf())
})
it('Millisecond', () => {
expect(dayjs().get('millisecond')).toBe(moment().get('millisecond'))
expect(dayjs().millisecond()).toBe(moment().millisecond())
expect(dayjs().millisecond(0).valueOf()).toBe(moment().millisecond(0).valueOf())
expect(dayjs().millisecond(1).valueOf()).toBe(moment().millisecond(1).valueOf())
})
it('Set Day', () => {
expect(dayjs().set('date', 30).valueOf()).toBe(moment().set('date', 30).valueOf())
})
it('Set Day of Week', () => {
expect(dayjs().set('day', 0).valueOf()).toBe(moment().set('day', 0).valueOf())
})
it('Set Month', () => {
expect(dayjs().set('month', 11).valueOf()).toBe(moment().set('month', 11).valueOf())
})
it('Set Year', () => {
expect(dayjs().set('year', 2008).valueOf()).toBe(moment().set('year', 2008).valueOf())
})
it('Set Hour', () => {
expect(dayjs().set('hour', 6).valueOf()).toBe(moment().set('hour', 6).valueOf())
})
it('Set Minute', () => {
expect(dayjs().set('minute', 59).valueOf()).toBe(moment().set('minute', 59).valueOf())
})
it('Set Second', () => {
expect(dayjs().set('second', 59).valueOf()).toBe(moment().set('second', 59).valueOf())
})
it('Set Millisecond', () => {
expect(dayjs().set('millisecond', 999).valueOf()).toBe(moment().set('millisecond', 999).valueOf())
})
it('Set Month and Year in last day of month', () => {
// 2011-07-31 -> 2011-02-28
const origin = dayjs('2011-07-31T14:48:00.000Z')
const setMonth = origin.set('month', 1)
expect(setMonth.month()).toBe(1)
expect(origin.date()).toBe(31)
expect(setMonth.date()).toBe(28)
// 2000-02-29 -> 2001-02-28
const origin2 = dayjs('2000-02-29T14:48:00.000Z')
const setYear = origin2.set('year', 2001)
expect(setYear.month()).toBe(1)
expect(origin2.date()).toBe(29)
expect(setYear.date()).toBe(28)
})
it('Set Unknown String', () => {
const newDate = dayjs().set('Unknown String', 1)
expect(newDate.valueOf())
.toBe(moment().set('Unknown String', 1).valueOf())
})
it('Immutable Set', () => {
const dayjsA = dayjs()
const dayjsB = dayjsA.set('year', 2011)
const momentA = moment()
const momentB = momentA.set('year', 2011)
expect(dayjsA.valueOf()).not.toBe(dayjsB.valueOf())
expect(momentA.valueOf()).toBe(momentB.valueOf())
})
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/test/locale.test.js | test/locale.test.js | import MockDate from 'mockdate'
import moment from 'moment'
import dayjs from '../src'
import es from '../src/locale/es'
beforeEach(() => {
MockDate.set(new Date())
})
afterEach(() => {
MockDate.reset()
})
const format = 'dddd D, MMMM'
const NOT_SUPPORTED_LOCALE_STRING = 'not_supported_locale_string'
it('Uses spanish locale through constructor', () => { // not recommend
expect(dayjs('2018-4-28', { locale: es })
.format(format))
.toBe('sábado 28, abril')
})
it('set locale for one instance only', () => {
expect(dayjs('2018-4-28')
.format(format))
.toBe('Saturday 28, April')
expect(dayjs('2018-4-28')
.locale(es).format(format))
.toBe('sábado 28, abril')
expect(dayjs('2018-4-28')
.format(format))
.toBe('Saturday 28, April')
})
it('set global locale', () => {
dayjs.locale('en')
expect(dayjs('2018-4-28').format(format))
.toBe('Saturday 28, April')
dayjs.locale(es)
expect(dayjs('2018-4-28').format(format))
.toBe('sábado 28, abril')
dayjs.locale('en')
expect(dayjs('2018-4-28').format(format))
.toBe('Saturday 28, April')
})
it('get instance locale name', () => {
expect(dayjs().locale()).toBe('en')
expect(dayjs().locale()).toBe(moment().locale())
expect(dayjs().locale('es').locale()).toBe('es')
expect(dayjs().locale('es').locale()).toBe(moment().locale('es').locale())
dayjs.locale(es)
moment.locale('es')
expect(dayjs().locale()).toBe('es')
expect(dayjs().locale()).toBe(moment().locale())
})
it('immutable instance locale', () => {
dayjs.locale('en')
const origin = dayjs('2018-4-28')
expect(origin.format(format))
.toBe('Saturday 28, April')
expect(origin.locale('es').format(format))
.toBe('sábado 28, abril')
const changed = origin.locale('es')
expect(changed.format(format))
.toBe('sábado 28, abril')
expect(origin.format(format))
.toBe('Saturday 28, April')
})
it('User custom locale', () => {
expect(dayjs('2018-4-28')
.locale('xx', {
weekdays: Array(7).fill('week'),
months: Array(12).fill('month')
})
.format(format))
.toBe('week 28, month')
})
describe('Instance locale inheritance', () => {
const esDayjs = dayjs('2018-4-28').locale(es)
it('Clone', () => {
expect(esDayjs.clone().format(format))
.toBe('sábado 28, abril')
expect(dayjs(esDayjs).format(format))
.toBe('sábado 28, abril')
})
it('StartOf EndOf', () => {
expect(esDayjs.startOf('year').format(format))
.toBe('lunes 1, enero')
expect(esDayjs.endOf('day').format(format))
.toBe('sábado 28, abril')
})
it('Set', () => {
expect(esDayjs.set('year', 2017).format(format))
.toBe('viernes 28, abril')
})
it('Add', () => {
expect(esDayjs.add(1, 'year').format(format))
.toBe('domingo 28, abril')
expect(esDayjs.add(1, 'month').format(format))
.toBe('lunes 28, mayo')
expect(esDayjs.add(1, 'minute').format(format))
.toBe('sábado 28, abril')
})
it('dayjs.locale() returns locale name', () => {
dayjs.locale(es)
moment.locale('es')
expect(dayjs.locale()).toBe(moment.locale())
dayjs.locale('en')
moment.locale('en')
expect(dayjs.locale()).toBe(moment.locale())
})
})
it('Not supported locale string fallback to previous one (instance)', () => {
const D = dayjs()
expect(D.locale()).toBe('en')
const D2 = D.locale(NOT_SUPPORTED_LOCALE_STRING)
expect(D2.locale()).toBe('en')
expect(D2.format()).toBe(D.format())
const D3 = D2.locale('es')
expect(D3.locale()).toBe('es')
const D4 = D3.locale(NOT_SUPPORTED_LOCALE_STRING)
expect(D4.locale()).toBe('es')
})
it('Not supported locale string fallback to previous one (global)', () => {
expect(dayjs().locale()).toBe('en')
dayjs.locale(NOT_SUPPORTED_LOCALE_STRING)
expect(dayjs().locale()).toBe('en')
dayjs.locale('es')
expect(dayjs().locale()).toBe('es')
dayjs.locale(NOT_SUPPORTED_LOCALE_STRING)
expect(dayjs().locale()).toBe('es')
})
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/test/comparison.test.js | test/comparison.test.js | import MockDate from 'mockdate'
import dayjs from '../src'
beforeEach(() => {
MockDate.set(new Date())
})
afterEach(() => {
MockDate.reset()
})
test('is same without units', () => {
const m = dayjs(new Date(2011, 3, 2, 3, 4, 5, 10))
const mCopy = dayjs(m)
expect(m.isSame(dayjs(new Date(2012, 3, 2, 3, 5, 5, 10)))).toBe(false, 'year is later')
expect(m.isSame(dayjs(new Date(2010, 3, 2, 3, 3, 5, 10)))).toBe(false, 'year is earlier')
expect(m.isSame(dayjs(new Date(2011, 4, 2, 3, 4, 5, 10)))).toBe(false, 'month is later')
expect(m.isSame(dayjs(new Date(2011, 2, 2, 3, 4, 5, 10)))).toBe(false, 'month is earlier')
expect(m.isSame(dayjs(new Date(2011, 3, 3, 3, 4, 5, 10)))).toBe(false, 'day is later')
expect(m.isSame(dayjs(new Date(2011, 3, 1, 3, 4, 5, 10)))).toBe(false, 'day is earlier')
expect(m.isSame(dayjs(new Date(2011, 3, 2, 4, 4, 5, 10)))).toBe(false, 'hour is later')
expect(m.isSame(dayjs(new Date(2011, 3, 2, 2, 4, 5, 10)))).toBe(false, 'hour is earlier')
expect(m.isSame(dayjs(new Date(2011, 3, 2, 3, 5, 5, 10)))).toBe(false, 'minute is later')
expect(m.isSame(dayjs(new Date(2011, 3, 2, 3, 3, 5, 10)))).toBe(false, 'minute is earlier')
expect(m.isSame(dayjs(new Date(2011, 3, 2, 3, 4, 6, 10)))).toBe(false, 'second is later')
expect(m.isSame(dayjs(new Date(2011, 3, 2, 3, 4, 4, 11)))).toBe(false, 'second is earlier')
expect(m.isSame(dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)))).toBe(true, 'millisecond match')
expect(m.isSame(dayjs(new Date(2011, 3, 2, 3, 4, 5, 11)))).toBe(false, 'millisecond is later')
expect(m.isSame(dayjs(new Date(2011, 3, 2, 3, 4, 5, 9)))).toBe(false, 'millisecond is earlier')
expect(m.isSame(m)).toBe(true, 'moments are the same as themselves')
expect(+m).toEqual(+mCopy, 'isSame second should not change moment')
})
test('is same year', () => {
const m = dayjs(new Date(2011, 1, 2, 3, 4, 5, 6))
const mCopy = dayjs(m)
expect(m.isSame(dayjs(new Date(2011, 5, 6, 7, 8, 9, 10)), 'year')).toBe(true, 'year match')
expect(m.isSame(dayjs(new Date(2011, 5, 6, 7, 8, 9, 10)), 'years')).toBe(true, 'plural should work')
expect(m.isSame(dayjs(new Date(2012, 5, 6, 7, 8, 9, 10)), 'year')).toBe(false, 'year mismatch')
expect(m.isSame(dayjs(new Date(2011, 0, 1, 0, 0, 0, 0)), 'year')).toBe(true, 'exact start of year')
expect(m.isSame(dayjs(new Date(2011, 11, 31, 23, 59, 59, 999)), 'year')).toBe(true, 'exact end of year')
expect(m.isSame(dayjs(new Date(2012, 0, 1, 0, 0, 0, 0)), 'year')).toBe(false, 'start of next year')
expect(m.isSame(dayjs(new Date(2010, 11, 31, 23, 59, 59, 999)), 'year')).toBe(false, 'end of previous year')
expect(m.isSame(m, 'year')).toBe(true, 'same moments are in the same year')
expect(+m).toEqual(+mCopy, 'isSame year should not change moment')
})
test('is same month', () => {
const m = dayjs(new Date(2011, 2, 3, 4, 5, 6, 7))
const mCopy = dayjs(m)
expect(m.isSame(dayjs(new Date(2011, 2, 6, 7, 8, 9, 10)), 'month')).toBe(true, 'month match')
expect(m.isSame(dayjs(new Date(2011, 2, 6, 7, 8, 9, 10)), 'months')).toBe(true, 'plural should work')
expect(m.isSame(dayjs(new Date(2012, 2, 6, 7, 8, 9, 10)), 'month')).toBe(false, 'year mismatch')
expect(m.isSame(dayjs(new Date(2011, 5, 6, 7, 8, 9, 10)), 'month')).toBe(false, 'month mismatch')
expect(m.isSame(dayjs(new Date(2011, 2, 1, 0, 0, 0, 0)), 'month')).toBe(true, 'exact start of month')
expect(m.isSame(dayjs(new Date(2011, 2, 31, 23, 59, 59, 999)), 'month')).toBe(true, 'exact end of month')
expect(m.isSame(dayjs(new Date(2011, 3, 1, 0, 0, 0, 0)), 'month')).toBe(false, 'start of next month')
expect(m.isSame(dayjs(new Date(2011, 1, 27, 23, 59, 59, 999)), 'month')).toBe(false, 'end of previous month')
expect(m.isSame(m, 'month')).toBe(true, 'same moments are in the same month')
expect(+m).toEqual(+mCopy, 'isSame month should not change moment')
})
test('is same day', () => {
const m = dayjs(new Date(2011, 1, 2, 3, 4, 5, 6))
const mCopy = dayjs(m)
expect(m.isSame(dayjs(new Date(2011, 1, 2, 7, 8, 9, 10)), 'day')).toBe(true, 'day match')
expect(m.isSame(dayjs(new Date(2011, 1, 2, 7, 8, 9, 10)), 'days')).toBe(true, 'plural should work')
expect(m.isSame(dayjs(new Date(2012, 1, 2, 7, 8, 9, 10)), 'day')).toBe(false, 'year mismatch')
expect(m.isSame(dayjs(new Date(2011, 2, 2, 7, 8, 9, 10)), 'day')).toBe(false, 'month mismatch')
expect(m.isSame(dayjs(new Date(2011, 1, 3, 7, 8, 9, 10)), 'day')).toBe(false, 'day mismatch')
expect(m.isSame(dayjs(new Date(2011, 1, 2, 0, 0, 0, 0)), 'day')).toBe(true, 'exact start of day')
expect(m.isSame(dayjs(new Date(2011, 1, 2, 23, 59, 59, 999)), 'day')).toBe(true, 'exact end of day')
expect(m.isSame(dayjs(new Date(2011, 1, 3, 0, 0, 0, 0)), 'day')).toBe(false, 'start of next day')
expect(m.isSame(dayjs(new Date(2011, 1, 1, 23, 59, 59, 999)), 'day')).toBe(false, 'end of previous day')
expect(m.isSame(m, 'day')).toBe(true, 'same moments are in the same day')
expect(+m).toEqual(+mCopy, 'isSame day should not change moment')
})
test('is same hour', () => {
const m = dayjs(new Date(2011, 1, 2, 3, 4, 5, 6))
const mCopy = dayjs(m)
expect(m.isSame(dayjs(new Date(2011, 1, 2, 3, 8, 9, 10)), 'hour')).toBe(true, 'hour match')
expect(m.isSame(dayjs(new Date(2011, 1, 2, 3, 8, 9, 10)), 'hours')).toBe(true, 'plural should work')
expect(m.isSame(dayjs(new Date(2012, 1, 2, 3, 8, 9, 10)), 'hour')).toBe(false, 'year mismatch')
expect(m.isSame(dayjs(new Date(2011, 2, 2, 3, 8, 9, 10)), 'hour')).toBe(false, 'month mismatch')
expect(m.isSame(dayjs(new Date(2011, 1, 3, 3, 8, 9, 10)), 'hour')).toBe(false, 'day mismatch')
expect(m.isSame(dayjs(new Date(2011, 1, 2, 4, 8, 9, 10)), 'hour')).toBe(false, 'hour mismatch')
expect(m.isSame(dayjs(new Date(2011, 1, 2, 3, 0, 0, 0)), 'hour')).toBe(true, 'exact start of hour')
expect(m.isSame(dayjs(new Date(2011, 1, 2, 3, 59, 59, 999)), 'hour')).toBe(true, 'exact end of hour')
expect(m.isSame(dayjs(new Date(2011, 1, 2, 4, 0, 0, 0)), 'hour')).toBe(false, 'start of next hour')
expect(m.isSame(dayjs(new Date(2011, 1, 2, 2, 59, 59, 999)), 'hour')).toBe(false, 'end of previous hour')
expect(m.isSame(m, 'hour')).toBe(true, 'same moments are in the same hour')
expect(+m).toEqual(+mCopy, 'isSame hour should not change moment')
})
test('is same minute', () => {
const m = dayjs(new Date(2011, 1, 2, 3, 4, 5, 6))
const mCopy = dayjs(m)
expect(m.isSame(dayjs(new Date(2011, 1, 2, 3, 4, 9, 10)), 'minute')).toBe(true, 'minute match')
expect(m.isSame(dayjs(new Date(2011, 1, 2, 3, 4, 9, 10)), 'minutes')).toBe(true, 'plural should work')
expect(m.isSame(dayjs(new Date(2012, 1, 2, 3, 4, 9, 10)), 'minute')).toBe(false, 'year mismatch')
expect(m.isSame(dayjs(new Date(2011, 2, 2, 3, 4, 9, 10)), 'minute')).toBe(false, 'month mismatch')
expect(m.isSame(dayjs(new Date(2011, 1, 3, 3, 4, 9, 10)), 'minute')).toBe(false, 'day mismatch')
expect(m.isSame(dayjs(new Date(2011, 1, 2, 4, 4, 9, 10)), 'minute')).toBe(false, 'hour mismatch')
expect(m.isSame(dayjs(new Date(2011, 1, 2, 3, 5, 9, 10)), 'minute')).toBe(false, 'minute mismatch')
expect(m.isSame(dayjs(new Date(2011, 1, 2, 3, 4, 0, 0)), 'minute')).toBe(true, 'exact start of minute')
expect(m.isSame(dayjs(new Date(2011, 1, 2, 3, 4, 59, 999)), 'minute')).toBe(true, 'exact end of minute')
expect(m.isSame(dayjs(new Date(2011, 1, 2, 3, 5, 0, 0)), 'minute')).toBe(false, 'start of next minute')
expect(m.isSame(dayjs(new Date(2011, 1, 2, 3, 3, 59, 999)), 'minute')).toBe(false, 'end of previous minute')
expect(m.isSame(m, 'minute')).toBe(true, 'same moments are in the same minute')
expect(+m).toEqual(+mCopy, 'isSame minute should not change moment')
})
test('is same second', () => {
const m = dayjs(new Date(2011, 1, 2, 3, 4, 5, 6))
const mCopy = dayjs(m)
expect(m.isSame(dayjs(new Date(2011, 1, 2, 3, 4, 5, 10)), 'second')).toBe(true, 'second match')
expect(m.isSame(dayjs(new Date(2011, 1, 2, 3, 4, 5, 10)), 'seconds')).toBe(true, 'plural should work')
expect(m.isSame(dayjs(new Date(2012, 1, 2, 3, 4, 5, 10)), 'second')).toBe(false, 'year mismatch')
expect(m.isSame(dayjs(new Date(2011, 2, 2, 3, 4, 5, 10)), 'second')).toBe(false, 'month mismatch')
expect(m.isSame(dayjs(new Date(2011, 1, 3, 3, 4, 5, 10)), 'second')).toBe(false, 'day mismatch')
expect(m.isSame(dayjs(new Date(2011, 1, 2, 4, 4, 5, 10)), 'second')).toBe(false, 'hour mismatch')
expect(m.isSame(dayjs(new Date(2011, 1, 2, 3, 5, 5, 10)), 'second')).toBe(false, 'minute mismatch')
expect(m.isSame(dayjs(new Date(2011, 1, 2, 3, 4, 6, 10)), 'second')).toBe(false, 'second mismatch')
expect(m.isSame(dayjs(new Date(2011, 1, 2, 3, 4, 5, 0)), 'second')).toBe(true, 'exact start of second')
expect(m.isSame(dayjs(new Date(2011, 1, 2, 3, 4, 5, 999)), 'second')).toBe(true, 'exact end of second')
expect(m.isSame(dayjs(new Date(2011, 1, 2, 3, 4, 6, 0)), 'second')).toBe(false, 'start of next second')
expect(m.isSame(dayjs(new Date(2011, 1, 2, 3, 4, 4, 999)), 'second')).toBe(false, 'end of previous second')
expect(m.isSame(m, 'second')).toBe(true, 'same moments are in the same second')
expect(+m).toEqual(+mCopy, 'isSame second should not change moment')
})
test('is same millisecond', () => {
const m = dayjs(new Date(2011, 3, 2, 3, 4, 5, 10))
const mCopy = dayjs(m)
expect(m.isSame(dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)), 'millisecond')).toBe(true, 'millisecond match')
expect(m.isSame(dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)), 'milliseconds')).toBe(true, 'plural should work')
expect(m.isSame(dayjs(new Date(2012, 3, 2, 3, 4, 5, 10)), 'millisecond')).toBe(false, 'year is later')
expect(m.isSame(dayjs(new Date(2010, 3, 2, 3, 4, 5, 10)), 'millisecond')).toBe(false, 'year is earlier')
expect(m.isSame(dayjs(new Date(2011, 4, 2, 3, 4, 5, 10)), 'millisecond')).toBe(false, 'month is later')
expect(m.isSame(dayjs(new Date(2011, 2, 2, 3, 4, 5, 10)), 'millisecond')).toBe(false, 'month is earlier')
expect(m.isSame(dayjs(new Date(2011, 3, 3, 3, 4, 5, 10)), 'millisecond')).toBe(false, 'day is later')
expect(m.isSame(dayjs(new Date(2011, 3, 1, 1, 4, 5, 10)), 'millisecond')).toBe(false, 'day is earlier')
expect(m.isSame(dayjs(new Date(2011, 3, 2, 4, 4, 5, 10)), 'millisecond')).toBe(false, 'hour is later')
expect(m.isSame(dayjs(new Date(2011, 3, 1, 4, 1, 5, 10)), 'millisecond')).toBe(false, 'hour is earlier')
expect(m.isSame(dayjs(new Date(2011, 3, 2, 3, 5, 5, 10)), 'millisecond')).toBe(false, 'minute is later')
expect(m.isSame(dayjs(new Date(2011, 3, 2, 3, 3, 5, 10)), 'millisecond')).toBe(false, 'minute is earlier')
expect(m.isSame(dayjs(new Date(2011, 3, 2, 3, 4, 6, 10)), 'millisecond')).toBe(false, 'second is later')
expect(m.isSame(dayjs(new Date(2011, 3, 2, 3, 4, 4, 5)), 'millisecond')).toBe(false, 'second is earlier')
expect(m.isSame(dayjs(new Date(2011, 3, 2, 3, 4, 6, 11)), 'millisecond')).toBe(false, 'millisecond is later')
expect(m.isSame(dayjs(new Date(2011, 3, 2, 3, 4, 4, 9)), 'millisecond')).toBe(false, 'millisecond is earlier')
expect(m.isSame(m, 'millisecond')).toBe(true, 'same moments are in the same millisecond')
expect(+m).toEqual(+mCopy, 'isSame millisecond should not change moment')
})
test('is same with invalid moments', () => {
expect(dayjs(null).isSame(dayjs('2018-01-01'))).toBe(false, 'invalid moments are not considered equal')
expect(dayjs('2018-01-01').isSame(dayjs(null))).toBe(false, 'invalid moments are not considered equal')
})
// isAfter()
test('is after year', () => {
const m = dayjs(new Date(2011, 1, 2, 3, 4, 5, 6))
const mCopy = dayjs(m)
expect(m.isAfter(dayjs(new Date(2011, 5, 6, 7, 8, 9, 10)), 'year')).toBe(false, 'year match')
expect(m.isAfter(dayjs(new Date(2010, 5, 6, 7, 8, 9, 10)), 'years')).toBe(true, 'plural should work')
expect(m.isAfter(dayjs(new Date(2013, 5, 6, 7, 8, 9, 10)), 'year')).toBe(false, 'year is later')
expect(m.isAfter(dayjs(new Date(2010, 5, 6, 7, 8, 9, 10)), 'year')).toBe(true, 'year is earlier')
expect(m.isAfter(dayjs(new Date(2011, 0, 1, 0, 0, 0, 0)), 'year')).toBe(false, 'exact start of year')
expect(m.isAfter(dayjs(new Date(2011, 11, 31, 23, 59, 59, 999)), 'year')).toBe(false, 'exact end of year')
expect(m.isAfter(dayjs(new Date(2012, 0, 1, 0, 0, 0, 0)), 'year')).toBe(false, 'start of next year')
expect(m.isAfter(dayjs(new Date(2010, 11, 31, 23, 59, 59, 999)), 'year')).toBe(true, 'end of previous year')
expect(m.isAfter(dayjs(new Date(1980, 11, 31, 23, 59, 59, 999)), 'year')).toBe(true, 'end of year far before')
expect(m.isAfter(m, 'year')).toBe(false, 'same moments are not after the same year')
expect(+m).toEqual(+mCopy, 'isAfter year should not change moment')
})
test('is after month', () => {
const m = dayjs(new Date(2011, 2, 3, 4, 5, 6, 7))
const mCopy = dayjs(m)
expect(m.isAfter(dayjs(new Date(2011, 2, 6, 7, 8, 9, 10)), 'month')).toBe(false, 'month match')
expect(m.isAfter(dayjs(new Date(2010, 2, 6, 7, 8, 9, 10)), 'months')).toBe(true, 'plural should work')
expect(m.isAfter(dayjs(new Date(2012, 2, 6, 7, 8, 9, 10)), 'month')).toBe(false, 'year is later')
expect(m.isAfter(dayjs(new Date(2010, 2, 6, 7, 8, 9, 10)), 'month')).toBe(true, 'year is earlier')
expect(m.isAfter(dayjs(new Date(2011, 5, 6, 7, 8, 9, 10)), 'month')).toBe(false, 'month is later')
expect(m.isAfter(dayjs(new Date(2011, 1, 6, 7, 8, 9, 10)), 'month')).toBe(true, 'month is earlier')
expect(m.isAfter(dayjs(new Date(2011, 2, 1, 0, 0, 0, 0)), 'month')).toBe(false, 'exact start of month')
expect(m.isAfter(dayjs(new Date(2011, 2, 31, 23, 59, 59, 999)), 'month')).toBe(false, 'exact end of month')
expect(m.isAfter(dayjs(new Date(2011, 3, 1, 0, 0, 0, 0)), 'month')).toBe(false, 'start of next month')
expect(m.isAfter(dayjs(new Date(2011, 1, 27, 23, 59, 59, 999)), 'month')).toBe(true, 'end of previous month')
expect(m.isAfter(dayjs(new Date(2010, 12, 31, 23, 59, 59, 999)), 'month')).toBe(true, 'later month but earlier year')
expect(m.isAfter(m, 'month')).toBe(false, 'same moments are not after the same month')
expect(+m).toEqual(+mCopy, 'isAfter month should not change moment')
})
test('is after day', () => {
const m = dayjs(new Date(2011, 3, 2, 3, 4, 5, 6))
const mCopy = dayjs(m)
expect(m.isAfter(dayjs(new Date(2011, 3, 2, 7, 8, 9, 10)), 'day')).toBe(false, 'day match')
expect(m.isAfter(dayjs(new Date(2010, 3, 2, 7, 8, 9, 10)), 'days')).toBe(true, 'plural should work')
expect(m.isAfter(dayjs(new Date(2012, 3, 2, 7, 8, 9, 10)), 'day')).toBe(false, 'year is later')
expect(m.isAfter(dayjs(new Date(2010, 3, 2, 7, 8, 9, 10)), 'day')).toBe(true, 'year is earlier')
expect(m.isAfter(dayjs(new Date(2011, 4, 2, 7, 8, 9, 10)), 'day')).toBe(false, 'month is later')
expect(m.isAfter(dayjs(new Date(2011, 2, 2, 7, 8, 9, 10)), 'day')).toBe(true, 'month is earlier')
expect(m.isAfter(dayjs(new Date(2011, 3, 3, 7, 8, 9, 10)), 'day')).toBe(false, 'day is later')
expect(m.isAfter(dayjs(new Date(2011, 3, 1, 7, 8, 9, 10)), 'day')).toBe(true, 'day is earlier')
expect(m.isAfter(dayjs(new Date(2011, 3, 2, 0, 0, 0, 0)), 'day')).toBe(false, 'exact start of day')
expect(m.isAfter(dayjs(new Date(2011, 3, 2, 23, 59, 59, 999)), 'day')).toBe(false, 'exact end of day')
expect(m.isAfter(dayjs(new Date(2011, 3, 3, 0, 0, 0, 0)), 'day')).toBe(false, 'start of next day')
expect(m.isAfter(dayjs(new Date(2011, 3, 1, 23, 59, 59, 999)), 'day')).toBe(true, 'end of previous day')
expect(m.isAfter(dayjs(new Date(2010, 3, 10, 0, 0, 0, 0)), 'day')).toBe(true, 'later day but earlier year')
expect(m.isAfter(m, 'day')).toBe(false, 'same moments are not after the same day')
expect(+m).toEqual(+mCopy, 'isAfter day should not change moment')
})
test('is after hour', () => {
const m = dayjs(new Date(2011, 3, 2, 3, 4, 5, 6))
const mCopy = dayjs(m)
expect(m.isAfter(dayjs(new Date(2011, 3, 2, 3, 8, 9, 10)), 'hour')).toBe(false, 'hour match')
expect(m.isAfter(dayjs(new Date(2010, 3, 2, 3, 8, 9, 10)), 'hours')).toBe(true, 'plural should work')
expect(m.isAfter(dayjs(new Date(2012, 3, 2, 3, 8, 9, 10)), 'hour')).toBe(false, 'year is later')
expect(m.isAfter(dayjs(new Date(2010, 3, 2, 3, 8, 9, 10)), 'hour')).toBe(true, 'year is earlier')
expect(m.isAfter(dayjs(new Date(2011, 4, 2, 3, 8, 9, 10)), 'hour')).toBe(false, 'month is later')
expect(m.isAfter(dayjs(new Date(2011, 1, 2, 3, 8, 9, 10)), 'hour')).toBe(true, 'month is earlier')
expect(m.isAfter(dayjs(new Date(2011, 3, 3, 3, 8, 9, 10)), 'hour')).toBe(false, 'day is later')
expect(m.isAfter(dayjs(new Date(2011, 3, 1, 3, 8, 9, 10)), 'hour')).toBe(true, 'day is earlier')
expect(m.isAfter(dayjs(new Date(2011, 3, 2, 4, 8, 9, 10)), 'hour')).toBe(false, 'hour is later')
expect(m.isAfter(dayjs(new Date(2011, 3, 2, 3, 8, 9, 10)), 'hour')).toBe(false, 'hour is earlier')
expect(m.isAfter(dayjs(new Date(2011, 3, 2, 3, 0, 0, 0)), 'hour')).toBe(false, 'exact start of hour')
expect(m.isAfter(dayjs(new Date(2011, 3, 2, 3, 59, 59, 999)), 'hour')).toBe(false, 'exact end of hour')
expect(m.isAfter(dayjs(new Date(2011, 3, 2, 4, 0, 0, 0)), 'hour')).toBe(false, 'start of next hour')
expect(m.isAfter(dayjs(new Date(2011, 3, 2, 2, 59, 59, 999)), 'hour')).toBe(true, 'end of previous hour')
expect(m.isAfter(m, 'hour')).toBe(false, 'same moments are not after the same hour')
expect(+m).toEqual(+mCopy, 'isAfter hour should not change moment')
})
test('is after minute', () => {
const m = dayjs(new Date(2011, 3, 2, 3, 4, 5, 6))
const mCopy = dayjs(m)
expect(m.isAfter(dayjs(new Date(2011, 3, 2, 3, 4, 9, 10)), 'minute')).toBe(false, 'minute match')
expect(m.isAfter(dayjs(new Date(2010, 3, 2, 3, 4, 9, 10)), 'minutes')).toBe(true, 'plural should work')
expect(m.isAfter(dayjs(new Date(2012, 3, 2, 3, 4, 9, 10)), 'minute')).toBe(false, 'year is later')
expect(m.isAfter(dayjs(new Date(2010, 3, 2, 3, 4, 9, 10)), 'minute')).toBe(true, 'year is earlier')
expect(m.isAfter(dayjs(new Date(2011, 4, 2, 3, 4, 9, 10)), 'minute')).toBe(false, 'month is later')
expect(m.isAfter(dayjs(new Date(2011, 2, 2, 3, 4, 9, 10)), 'minute')).toBe(true, 'month is earlier')
expect(m.isAfter(dayjs(new Date(2011, 3, 3, 3, 4, 9, 10)), 'minute')).toBe(false, 'day is later')
expect(m.isAfter(dayjs(new Date(2011, 3, 1, 3, 4, 9, 10)), 'minute')).toBe(true, 'day is earlier')
expect(m.isAfter(dayjs(new Date(2011, 3, 2, 4, 4, 9, 10)), 'minute')).toBe(false, 'hour is later')
expect(m.isAfter(dayjs(new Date(2011, 3, 2, 2, 4, 9, 10)), 'minute')).toBe(true, 'hour is earler')
expect(m.isAfter(dayjs(new Date(2011, 3, 2, 3, 5, 9, 10)), 'minute')).toBe(false, 'minute is later')
expect(m.isAfter(dayjs(new Date(2011, 3, 2, 3, 3, 9, 10)), 'minute')).toBe(true, 'minute is earlier')
expect(m.isAfter(dayjs(new Date(2011, 3, 2, 3, 4, 0, 0)), 'minute')).toBe(false, 'exact start of minute')
expect(m.isAfter(dayjs(new Date(2011, 3, 2, 3, 4, 59, 999)), 'minute')).toBe(false, 'exact end of minute')
expect(m.isAfter(dayjs(new Date(2011, 3, 2, 3, 5, 0, 0)), 'minute')).toBe(false, 'start of next minute')
expect(m.isAfter(dayjs(new Date(2011, 3, 2, 3, 3, 59, 999)), 'minute')).toBe(true, 'end of previous minute')
expect(m.isAfter(m, 'minute')).toBe(false, 'same moments are not after the same minute')
expect(+m).toEqual(+mCopy, 'isAfter minute should not change moment')
})
test('is after second', () => {
const m = dayjs(new Date(2011, 3, 2, 3, 4, 5, 10))
const mCopy = dayjs(m)
expect(m.isAfter(dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)), 'second')).toBe(false, 'second match')
expect(m.isAfter(dayjs(new Date(2010, 3, 2, 3, 4, 5, 10)), 'seconds')).toBe(true, 'plural should work')
expect(m.isAfter(dayjs(new Date(2012, 3, 2, 3, 4, 5, 10)), 'second')).toBe(false, 'year is later')
expect(m.isAfter(dayjs(new Date(2010, 3, 2, 3, 4, 5, 10)), 'second')).toBe(true, 'year is earlier')
expect(m.isAfter(dayjs(new Date(2011, 4, 2, 3, 4, 5, 10)), 'second')).toBe(false, 'month is later')
expect(m.isAfter(dayjs(new Date(2011, 2, 2, 3, 4, 5, 10)), 'second')).toBe(true, 'month is earlier')
expect(m.isAfter(dayjs(new Date(2011, 3, 3, 3, 4, 5, 10)), 'second')).toBe(false, 'day is later')
expect(m.isAfter(dayjs(new Date(2011, 3, 1, 1, 4, 5, 10)), 'second')).toBe(true, 'day is earlier')
expect(m.isAfter(dayjs(new Date(2011, 3, 2, 4, 4, 5, 10)), 'second')).toBe(false, 'hour is later')
expect(m.isAfter(dayjs(new Date(2011, 3, 1, 4, 1, 5, 10)), 'second')).toBe(true, 'hour is earlier')
expect(m.isAfter(dayjs(new Date(2011, 3, 2, 3, 5, 5, 10)), 'second')).toBe(false, 'minute is later')
expect(m.isAfter(dayjs(new Date(2011, 3, 2, 3, 3, 5, 10)), 'second')).toBe(true, 'minute is earlier')
expect(m.isAfter(dayjs(new Date(2011, 3, 2, 3, 4, 6, 10)), 'second')).toBe(false, 'second is later')
expect(m.isAfter(dayjs(new Date(2011, 3, 2, 3, 4, 4, 5)), 'second')).toBe(true, 'second is earlier')
expect(m.isAfter(dayjs(new Date(2011, 3, 2, 3, 4, 5, 0)), 'second')).toBe(false, 'exact start of second')
expect(m.isAfter(dayjs(new Date(2011, 3, 2, 3, 4, 5, 999)), 'second')).toBe(false, 'exact end of second')
expect(m.isAfter(dayjs(new Date(2011, 3, 2, 3, 4, 6, 0)), 'second')).toBe(false, 'start of next second')
expect(m.isAfter(dayjs(new Date(2011, 3, 2, 3, 4, 4, 999)), 'second')).toBe(true, 'end of previous second')
expect(m.isAfter(m, 'second')).toBe(false, 'same moments are not after the same second')
expect(+m).toEqual(+mCopy, 'isAfter second should not change moment')
})
test('is after millisecond', () => {
const m = dayjs(new Date(2011, 3, 2, 3, 4, 5, 10))
const mCopy = dayjs(m)
expect(m.isAfter(dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)), 'millisecond')).toBe(false, 'millisecond match')
expect(m.isAfter(dayjs(new Date(2010, 3, 2, 3, 4, 5, 10)), 'milliseconds')).toBe(true, 'plural should work')
expect(m.isAfter(dayjs(new Date(2012, 3, 2, 3, 4, 5, 10)), 'millisecond')).toBe(false, 'year is later')
expect(m.isAfter(dayjs(new Date(2010, 3, 2, 3, 4, 5, 10)), 'millisecond')).toBe(true, 'year is earlier')
expect(m.isAfter(dayjs(new Date(2011, 4, 2, 3, 4, 5, 10)), 'millisecond')).toBe(false, 'month is later')
expect(m.isAfter(dayjs(new Date(2011, 2, 2, 3, 4, 5, 10)), 'millisecond')).toBe(true, 'month is earlier')
expect(m.isAfter(dayjs(new Date(2011, 3, 3, 3, 4, 5, 10)), 'millisecond')).toBe(false, 'day is later')
expect(m.isAfter(dayjs(new Date(2011, 3, 1, 1, 4, 5, 10)), 'millisecond')).toBe(true, 'day is earlier')
expect(m.isAfter(dayjs(new Date(2011, 3, 2, 4, 4, 5, 10)), 'millisecond')).toBe(false, 'hour is later')
expect(m.isAfter(dayjs(new Date(2011, 3, 1, 4, 1, 5, 10)), 'millisecond')).toBe(true, 'hour is earlier')
expect(m.isAfter(dayjs(new Date(2011, 3, 2, 3, 5, 5, 10)), 'millisecond')).toBe(false, 'minute is later')
expect(m.isAfter(dayjs(new Date(2011, 3, 2, 3, 3, 5, 10)), 'millisecond')).toBe(true, 'minute is earlier')
expect(m.isAfter(dayjs(new Date(2011, 3, 2, 3, 4, 6, 10)), 'millisecond')).toBe(false, 'second is later')
expect(m.isAfter(dayjs(new Date(2011, 3, 2, 3, 4, 4, 5)), 'millisecond')).toBe(true, 'second is earlier')
expect(m.isAfter(dayjs(new Date(2011, 3, 2, 3, 4, 6, 11)), 'millisecond')).toBe(false, 'millisecond is later')
expect(m.isAfter(dayjs(new Date(2011, 3, 2, 3, 4, 4, 9)), 'millisecond')).toBe(true, 'millisecond is earlier')
expect(m.isAfter(m, 'millisecond')).toBe(false, 'same moments are not after the same millisecond')
expect(+m).toEqual(+mCopy, 'isAfter millisecond should not change moment')
})
test('is after without units', () => {
const m = dayjs(new Date(2011, 3, 2, 3, 4, 5, 10))
const mCopy = dayjs(m)
expect(m.isAfter(dayjs(new Date(2012, 3, 2, 3, 5, 5, 10)))).toBe(false, 'year is later')
expect(m.isAfter(dayjs(new Date(2010, 3, 2, 3, 3, 5, 10)))).toBe(true, 'year is earlier')
expect(m.isAfter(dayjs(new Date(2011, 4, 2, 3, 4, 5, 10)))).toBe(false, 'month is later')
expect(m.isAfter(dayjs(new Date(2011, 2, 2, 3, 4, 5, 10)))).toBe(true, 'month is earlier')
expect(m.isAfter(dayjs(new Date(2011, 3, 3, 3, 4, 5, 10)))).toBe(false, 'day is later')
expect(m.isAfter(dayjs(new Date(2011, 3, 1, 3, 4, 5, 10)))).toBe(true, 'day is earlier')
expect(m.isAfter(dayjs(new Date(2011, 3, 2, 4, 4, 5, 10)))).toBe(false, 'hour is later')
expect(m.isAfter(dayjs(new Date(2011, 3, 2, 2, 4, 5, 10)))).toBe(true, 'hour is earlier')
expect(m.isAfter(dayjs(new Date(2011, 3, 2, 3, 5, 5, 10)))).toBe(false, 'minute is later')
expect(m.isAfter(dayjs(new Date(2011, 3, 2, 3, 3, 5, 10)))).toBe(true, 'minute is earlier')
expect(m.isAfter(dayjs(new Date(2011, 3, 2, 3, 4, 6, 10)))).toBe(false, 'second is later')
expect(m.isAfter(dayjs(new Date(2011, 3, 2, 3, 4, 4, 11)))).toBe(true, 'second is earlier')
expect(m.isAfter(dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)))).toBe(false, 'millisecond match')
expect(m.isAfter(dayjs(new Date(2011, 3, 2, 3, 4, 5, 11)))).toBe(false, 'millisecond is later')
expect(m.isAfter(dayjs(new Date(2011, 3, 2, 3, 4, 5, 9)))).toBe(true, 'millisecond is earlier')
expect(m.isAfter(m)).toBe(false, 'moments are not after themselves')
expect(+m).toEqual(+mCopy, 'isAfter second should not change moment')
})
test('is after invalid', () => {
const m = dayjs()
const invalid = dayjs(null)
expect(m.isAfter(invalid)).toBe(false, 'valid moment is not after invalid moment')
expect(invalid.isAfter(m)).toBe(false, 'invalid moment is not after valid moment')
expect(m.isAfter(invalid, 'year')).toBe(false, 'invalid moment year')
expect(m.isAfter(invalid, 'month')).toBe(false, 'invalid moment month')
expect(m.isAfter(invalid, 'day')).toBe(false, 'invalid moment day')
expect(m.isAfter(invalid, 'hour')).toBe(false, 'invalid moment hour')
expect(m.isAfter(invalid, 'minute')).toBe(false, 'invalid moment minute')
expect(m.isAfter(invalid, 'second')).toBe(false, 'invalid moment second')
expect(m.isAfter(invalid, 'milliseconds')).toBe(false, 'invalid moment milliseconds')
})
// isBefore()
test('is after without units', () => {
const m = dayjs(new Date(2011, 3, 2, 3, 4, 5, 10))
const mCopy = dayjs(m)
expect(m.isBefore(dayjs(new Date(2012, 3, 2, 3, 5, 5, 10)))).toBe(true, 'year is later')
expect(m.isBefore(dayjs(new Date(2010, 3, 2, 3, 3, 5, 10)))).toBe(false, 'year is earlier')
expect(m.isBefore(dayjs(new Date(2011, 4, 2, 3, 4, 5, 10)))).toBe(true, 'month is later')
expect(m.isBefore(dayjs(new Date(2011, 2, 2, 3, 4, 5, 10)))).toBe(false, 'month is earlier')
expect(m.isBefore(dayjs(new Date(2011, 3, 3, 3, 4, 5, 10)))).toBe(true, 'day is later')
expect(m.isBefore(dayjs(new Date(2011, 3, 1, 3, 4, 5, 10)))).toBe(false, 'day is earlier')
expect(m.isBefore(dayjs(new Date(2011, 3, 2, 4, 4, 5, 10)))).toBe(true, 'hour is later')
expect(m.isBefore(dayjs(new Date(2011, 3, 2, 2, 4, 5, 10)))).toBe(false, 'hour is earlier')
expect(m.isBefore(dayjs(new Date(2011, 3, 2, 3, 5, 5, 10)))).toBe(true, 'minute is later')
expect(m.isBefore(dayjs(new Date(2011, 3, 2, 3, 3, 5, 10)))).toBe(false, 'minute is earlier')
expect(m.isBefore(dayjs(new Date(2011, 3, 2, 3, 4, 6, 10)))).toBe(true, 'second is later')
expect(m.isBefore(dayjs(new Date(2011, 3, 2, 3, 4, 4, 11)))).toBe(false, 'second is earlier')
expect(m.isBefore(dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)))).toBe(false, 'millisecond match')
expect(m.isBefore(dayjs(new Date(2011, 3, 2, 3, 4, 5, 11)))).toBe(true, 'millisecond is later')
expect(m.isBefore(dayjs(new Date(2011, 3, 2, 3, 4, 5, 9)))).toBe(false, 'millisecond is earlier')
expect(m.isBefore(m)).toBe(false, 'moments are not before themselves')
expect(+m).toEqual(+mCopy, 'isBefore second should not change moment')
})
test('is before year', () => {
const m = dayjs(new Date(2011, 1, 2, 3, 4, 5, 6))
const mCopy = dayjs(m)
expect(m.isBefore(dayjs(new Date(2011, 5, 6, 7, 8, 9, 10)), 'year')).toBe(false, 'year match')
expect(m.isBefore(dayjs(new Date(2012, 5, 6, 7, 8, 9, 10)), 'years')).toBe(true, 'plural should work')
expect(m.isBefore(dayjs(new Date(2013, 5, 6, 7, 8, 9, 10)), 'year')).toBe(true, 'year is later')
expect(m.isBefore(dayjs(new Date(2010, 5, 6, 7, 8, 9, 10)), 'year')).toBe(false, 'year is earlier')
expect(m.isBefore(dayjs(new Date(2011, 0, 1, 0, 0, 0, 0)), 'year')).toBe(false, 'exact start of year')
expect(m.isBefore(dayjs(new Date(2011, 11, 31, 23, 59, 59, 999)), 'year')).toBe(false, 'exact end of year')
expect(m.isBefore(dayjs(new Date(2012, 0, 1, 0, 0, 0, 0)), 'year')).toBe(true, 'start of next year')
expect(m.isBefore(dayjs(new Date(2010, 11, 31, 23, 59, 59, 999)), 'year')).toBe(false, 'end of previous year')
expect(m.isBefore(dayjs(new Date(1980, 11, 31, 23, 59, 59, 999)), 'year')).toBe(false, 'end of year far before')
expect(m.isBefore(m, 'year')).toBe(false, 'same moments are not before the same year')
expect(+m).toEqual(+mCopy, 'isBefore year should not change moment')
})
test('is before month', () => {
const m = dayjs(new Date(2011, 2, 3, 4, 5, 6, 7))
const mCopy = dayjs(m)
expect(m.isBefore(dayjs(new Date(2011, 2, 6, 7, 8, 9, 10)), 'month')).toBe(false, 'month match')
expect(m.isBefore(dayjs(new Date(2012, 2, 6, 7, 8, 9, 10)), 'months')).toBe(true, 'plural should work')
expect(m.isBefore(dayjs(new Date(2012, 2, 6, 7, 8, 9, 10)), 'month')).toBe(true, 'year is later')
expect(m.isBefore(dayjs(new Date(2010, 2, 6, 7, 8, 9, 10)), 'month')).toBe(false, 'year is earlier')
expect(m.isBefore(dayjs(new Date(2011, 5, 6, 7, 8, 9, 10)), 'month')).toBe(true, 'month is later')
expect(m.isBefore(dayjs(new Date(2011, 1, 6, 7, 8, 9, 10)), 'month')).toBe(false, 'month is earlier')
expect(m.isBefore(dayjs(new Date(2011, 2, 1, 0, 0, 0, 0)), 'month')).toBe(false, 'exact start of month')
expect(m.isBefore(dayjs(new Date(2011, 2, 31, 23, 59, 59, 999)), 'month')).toBe(false, 'exact end of month')
expect(m.isBefore(dayjs(new Date(2011, 3, 1, 0, 0, 0, 0)), 'month')).toBe(true, 'start of next month')
expect(m.isBefore(dayjs(new Date(2011, 1, 27, 23, 59, 59, 999)), 'month')).toBe(false, 'end of previous month')
expect(m.isBefore(dayjs(new Date(2010, 12, 31, 23, 59, 59, 999)), 'month')).toBe(false, 'later month but earlier year')
expect(m.isBefore(m, 'month')).toBe(false, 'same moments are not before the same month')
expect(+m).toEqual(+mCopy, 'isBefore month should not change moment')
})
test('is before day', () => {
const m = dayjs(new Date(2011, 3, 2, 3, 4, 5, 6))
const mCopy = dayjs(m)
expect(m.isBefore(dayjs(new Date(2011, 3, 2, 7, 8, 9, 10)), 'day')).toBe(false, 'day match')
expect(m.isBefore(dayjs(new Date(2012, 3, 2, 7, 8, 9, 10)), 'days')).toBe(true, 'plural should work')
expect(m.isBefore(dayjs(new Date(2012, 3, 2, 7, 8, 9, 10)), 'day')).toBe(true, 'year is later')
expect(m.isBefore(dayjs(new Date(2010, 3, 2, 7, 8, 9, 10)), 'day')).toBe(false, 'year is earlier')
expect(m.isBefore(dayjs(new Date(2011, 4, 2, 7, 8, 9, 10)), 'day')).toBe(true, 'month is later')
expect(m.isBefore(dayjs(new Date(2011, 2, 2, 7, 8, 9, 10)), 'day')).toBe(false, 'month is earlier')
expect(m.isBefore(dayjs(new Date(2011, 3, 3, 7, 8, 9, 10)), 'day')).toBe(true, 'day is later')
expect(m.isBefore(dayjs(new Date(2011, 3, 1, 7, 8, 9, 10)), 'day')).toBe(false, 'day is earlier')
expect(m.isBefore(dayjs(new Date(2011, 3, 2, 0, 0, 0, 0)), 'day')).toBe(false, 'exact start of day')
expect(m.isBefore(dayjs(new Date(2011, 3, 2, 23, 59, 59, 999)), 'day')).toBe(false, 'exact end of day')
expect(m.isBefore(dayjs(new Date(2011, 3, 3, 0, 0, 0, 0)), 'day')).toBe(true, 'start of next day')
expect(m.isBefore(dayjs(new Date(2011, 3, 1, 23, 59, 59, 999)), 'day')).toBe(false, 'end of previous day')
expect(m.isBefore(dayjs(new Date(2010, 3, 10, 0, 0, 0, 0)), 'day')).toBe(false, 'later day but earlier year')
expect(m.isBefore(m, 'day')).toBe(false, 'same moments are not before the same day')
expect(+m).toEqual(+mCopy, 'isBefore day should not change moment')
})
test('is before hour', () => {
const m = dayjs(new Date(2011, 3, 2, 3, 4, 5, 6))
const mCopy = dayjs(m)
expect(m.isBefore(dayjs(new Date(2011, 3, 2, 3, 8, 9, 10)), 'hour')).toBe(false, 'hour match')
expect(m.isBefore(dayjs(new Date(2012, 3, 2, 3, 8, 9, 10)), 'hours')).toBe(true, 'plural should work')
expect(m.isBefore(dayjs(new Date(2012, 3, 2, 3, 8, 9, 10)), 'hour')).toBe(true, 'year is later')
expect(m.isBefore(dayjs(new Date(2010, 3, 2, 3, 8, 9, 10)), 'hour')).toBe(false, 'year is earlier')
expect(m.isBefore(dayjs(new Date(2011, 4, 2, 3, 8, 9, 10)), 'hour')).toBe(true, 'month is later')
expect(m.isBefore(dayjs(new Date(2011, 1, 2, 3, 8, 9, 10)), 'hour')).toBe(false, 'month is earlier')
expect(m.isBefore(dayjs(new Date(2011, 3, 3, 3, 8, 9, 10)), 'hour')).toBe(true, 'day is later')
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | true |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/test/query.test.js | test/query.test.js | import MockDate from 'mockdate'
import dayjs from '../src'
beforeEach(() => {
MockDate.set(new Date())
})
afterEach(() => {
MockDate.reset()
})
describe('Is Before Is After Is Same', () => {
it('Compare to dayjs object', () => {
const dayA = dayjs()
const dayB = dayA.clone().add(1, 'day')
const dayC = dayA.clone().subtract(1, 'day')
expect(dayC.isBefore(dayA)).toBe(true)
expect(dayA.isSame(dayjs())).toBe(true)
expect(dayB.isAfter(dayA)).toBe(true)
expect(dayA.isSame()).toBe(true)
expect(dayB.isAfter()).toBe(true)
expect(dayC.isBefore()).toBe(true)
})
it('No value', () => {
const dayA = dayjs()
const dayB = dayA.clone().add(1, 'day')
const dayC = dayA.clone().subtract(1, 'day')
expect(dayA.isSame()).toBe(true)
expect(dayB.isAfter()).toBe(true)
expect(dayC.isBefore()).toBe(true)
})
it('With string', () => {
const dayD = dayjs()
expect(dayD.isSame('20180101')).toBe(false)
expect(dayD.isAfter('20180101')).toBe(true)
expect(dayD.isBefore('20180101')).toBe(false)
})
})
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/test/display.test.js | test/display.test.js | import moment from 'moment'
import MockDate from 'mockdate'
import dayjs from '../src'
import th from '../src/locale/th'
import '../src/locale/ja'
beforeEach(() => {
MockDate.set(new Date())
})
afterEach(() => {
MockDate.reset()
})
it('Format no formatStr', () => {
expect(dayjs().format()).toBe(moment().format())
})
it('Format invalid date', () => {
expect(dayjs('').format()).toBe(new Date('').toString())
expect(dayjs('otherString').format()).toBe(new Date('otherString').toString())
})
it('Format Year YY YYYY', () => {
expect(dayjs().format('YY')).toBe(moment().format('YY'))
expect(dayjs().format('YYYY')).toBe(moment().format('YYYY'))
})
it('Format Month M MM MMM MMMM', () => {
expect(dayjs().format('M')).toBe(moment().format('M'))
expect(dayjs().format('MM')).toBe(moment().format('MM'))
expect(dayjs().format('MMM')).toBe(moment().format('MMM'))
expect(dayjs().format('MMMM')).toBe(moment().format('MMMM'))
})
it('Format Day of Month D DD 1 - 31', () => {
expect(dayjs().format('D')).toBe(moment().format('D'))
expect(dayjs().format('DD')).toBe(moment().format('DD'))
})
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'))
})
it('Format Hour H HH 24-hour', () => {
expect(dayjs().format('H')).toBe(moment().format('H'))
expect(dayjs().format('HH')).toBe(moment().format('HH'))
})
it('Format Hour h hh 12-hour', () => {
const time = '2018-05-02T00:00:00.000'
const expected = '12'
expect(dayjs(time).format('h')).toBe(expected)
expect(dayjs(time).format('h')).toBe(moment(time).format('h'))
expect(dayjs(time).format('hh')).toBe(expected)
expect(dayjs(time).format('hh')).toBe(moment(time).format('hh'))
const time2 = '2018-05-02T01:00:00.000'
expect(dayjs(time2).format('h')).toBe(moment(time2).format('h'))
expect(dayjs(time2).format('h')).toBe('1')
expect(dayjs(time2).format('hh')).toBe(moment(time2).format('hh'))
expect(dayjs(time2).format('hh')).toBe('01')
const time3 = '2018-05-02T23:00:00.000'
const expected3 = '11'
expect(dayjs(time3).format('h')).toBe(moment(time3).format('h'))
expect(dayjs(time3).format('h')).toBe(expected3)
expect(dayjs(time3).format('hh')).toBe(moment(time3).format('hh'))
expect(dayjs(time3).format('hh')).toBe(expected3)
})
it('Format meridiens a A am / pm', () => {
const time = '2018-05-02T01:00:00.000'
expect(dayjs(time).format('a')).toBe('am')
expect(dayjs(time).format('a')).toBe(moment(time).format('a'))
expect(dayjs(time).format('A')).toBe('AM')
expect(dayjs(time).format('A')).toBe(moment(time).format('A'))
expect(dayjs(time).locale('ja').format('a')).toBe('午前')
expect(dayjs(time).locale('ja').format('a'))
.toBe(moment(time).locale('ja').format('a'))
const time2 = '2018-05-02T23:00:00.000'
expect(dayjs(time2).format('a')).toBe('pm')
expect(dayjs(time2).format('a')).toBe(moment(time2).format('a'))
expect(dayjs(time2).format('A')).toBe('PM')
expect(dayjs(time2).format('A')).toBe(moment(time2).format('A'))
expect(dayjs(time2).locale('ja').format('a')).toBe('午後')
expect(dayjs(time2).locale('ja').format('a'))
.toBe(moment(time2).locale('ja').format('a'))
})
it('Format Minute m mm', () => {
expect(dayjs().format('m')).toBe(moment().format('m'))
expect(dayjs().format('mm')).toBe(moment().format('mm'))
})
it('Format Second s ss SSS', () => {
expect(dayjs().format('s')).toBe(moment().format('s'))
expect(dayjs().format('ss')).toBe(moment().format('ss'))
expect(dayjs().format('SSS')).toBe(moment().format('SSS'))
const date = '2011-11-05T14:48:01.002Z'
expect(dayjs(date).format('s-ss-SSS')).toBe(moment(date).format('s-ss-SSS'))
})
it('Format Time Zone ZZ', () => {
MockDate.set(new Date('2018-05-02T23:00:00.000'), 60 * 8)
expect(dayjs().format('Z')).toBe(moment().format('Z'))
expect(dayjs().format('ZZ')).toBe(moment().format('ZZ'))
MockDate.set(new Date('2018-05-02T23:00:00.000'), 60 * 8 * -1)
expect(dayjs().format('ZZ')).toBe(moment().format('ZZ'))
MockDate.set(new Date('2018-05-02T23:00:00.000'), 0)
expect(dayjs().format('ZZ')).toBe(moment().format('ZZ'))
MockDate.set(new Date('2018-05-02T23:00:00.000'), 60 * 10)
expect(dayjs().format('ZZ')).toBe(moment().format('ZZ'))
MockDate.set(new Date('2018-05-02T23:00:00.000'), 60 * 11 * -1)
expect(dayjs().format('ZZ')).toBe(moment().format('ZZ'))
MockDate.set(new Date('2018-05-02T23:00:00.000'), 60 * 5.5 * -1)
expect(dayjs().format('ZZ')).toBe(moment().format('ZZ'))
})
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'))
})
it('Format token value is 0', () => {
const sundayDate = '2000-01-02'
const sundayStr = 'd H m s'
expect(dayjs(sundayDate).format(sundayStr))
.toBe(moment(sundayDate).format(sundayStr))
})
it('Format Complex with other string - : / ', () => {
const string = 'YY-M-D / HH:mm:ss'
expect(dayjs().format(string)).toBe(moment().format(string))
})
it('Format Escaping characters', () => {
let string = '[Z] Z'
expect(dayjs().format(string)).toBe(moment().format(string))
string = '[Z] Z [Z]'
expect(dayjs().format(string)).toBe(moment().format(string))
})
describe('Difference', () => {
it('empty -> default milliseconds', () => {
const dateString = '20110101'
const dayjsA = dayjs()
const dayjsB = dayjs(dateString)
const momentA = moment()
const momentB = moment(dateString)
expect(dayjsA.diff(dayjsB)).toBe(momentA.diff(momentB))
})
it('diff -> none dayjs object', () => {
const dateString = '2013-02-08'
const dayjsA = dayjs()
const dayjsB = new Date(dateString)
const momentA = moment()
const momentB = new Date(dateString)
expect(dayjsA.diff(dayjsB)).toBe(momentA.diff(momentB))
})
it('diff -> in seconds, minutes, hours, days, weeks, months, quarters, years ', () => {
const dayjsA = dayjs()
const dayjsB = dayjs().add(1000, 'days')
const dayjsC = dayjs().subtract(1000, 'days')
const momentA = moment()
const momentB = moment().add(1000, 'days')
const momentC = moment().subtract(1000, 'days')
const units = ['seconds', 'minutes', 'hours', 'days', 'weeks', 'months', 'quarters', 'years']
units.forEach((unit) => {
expect(dayjsA.diff(dayjsB, unit)).toBe(momentA.diff(momentB, unit))
expect(dayjsA.diff(dayjsB, unit, true)).toBe(momentA.diff(momentB, unit, true))
expect(dayjsA.diff(dayjsC, unit)).toBe(momentA.diff(momentC, unit))
expect(dayjsA.diff(dayjsC, unit, true)).toBe(momentA.diff(momentC, unit, true))
})
})
it('Special diff in month according to moment.js', () => {
const dayjsA = dayjs('20160115')
const dayjsB = dayjs('20160215')
const dayjsC = dayjs('20170115')
const momentA = moment('20160115')
const momentB = moment('20160215')
const momentC = moment('20170115')
const units = ['months', 'quarters', 'years']
units.forEach((unit) => {
expect(dayjsA.diff(dayjsB, unit)).toBe(momentA.diff(momentB, unit))
expect(dayjsA.diff(dayjsB, unit, true)).toBe(momentA.diff(momentB, unit, true))
expect(dayjsA.diff(dayjsC, unit)).toBe(momentA.diff(momentC, unit))
expect(dayjsA.diff(dayjsC, unit, true)).toBe(momentA.diff(momentC, unit, true))
})
})
it('MonthDiff', () => {
expect(dayjs('2018-08-08').diff(dayjs('2018-08-08'), 'month')).toEqual(0)
expect(dayjs('2018-09-08').diff(dayjs('2018-08-08'), 'month')).toEqual(1)
expect(dayjs('2018-08-08').diff(dayjs('2018-09-08'), 'month')).toEqual(-1)
expect(dayjs('2018-01-01').diff(dayjs('2018-01-01'), 'month')).toEqual(0)
})
it('undefined edge case', () => {
expect(dayjs().diff(undefined, 'seconds')).toBeDefined()
})
})
it('Unix Timestamp (milliseconds)', () => {
expect(dayjs().valueOf()).toBe(moment().valueOf())
})
it('Unix Timestamp (seconds)', () => {
expect(dayjs().unix()).toBe(moment().unix())
})
it('Days in Month', () => {
expect(dayjs().daysInMonth()).toBe(moment().daysInMonth())
expect(dayjs('20140201').daysInMonth()).toBe(moment('20140201').daysInMonth())
})
it('Utc Offset', () => {
expect(dayjs('2013-01-01T00:00:00.000').utcOffset()).toBe(moment('2013-01-01T00:00:00.000').utcOffset())
expect(dayjs('2013-01-01T05:00:00.000').utcOffset()).toBe(moment('2013-01-01T05:00:00.000').utcOffset())
})
it('As Javascript Date -> toDate', () => {
const base = dayjs()
const momentBase = moment()
const jsDate = base.toDate()
expect(jsDate).toEqual(momentBase.toDate())
expect(jsDate).toEqual(new Date())
jsDate.setFullYear(1970)
expect(jsDate.toUTCString()).not.toBe(base.toString())
})
it('As JSON -> toJSON', () => {
expect(dayjs().toJSON()).toBe(moment().toJSON())
global.console.warn = jest.genMockFunction()// moment.js otherString will throw warn
expect(dayjs('otherString').toJSON()).toBe(moment('otherString').toJSON())
expect(dayjs('otherString').toJSON()).toBe(null)
})
it('As ISO 8601 String -> toISOString e.g. 2013-02-04T22:44:30.652Z', () => {
expect(dayjs().toISOString()).toBe(moment().toISOString())
})
it('Year 1 formatted with YYYY should pad with zeroes', () => {
const date = new Date(1, 0, 1)
date.setUTCFullYear(1) // Required because 0-99 are parsed as 19xx in JS: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/Date#year
const res = dayjs(date).format('YYYY')
expect(res.slice(0, 3)).toBe('000') // because of timezone, the result might be 0000 0001 or 0002
expect(res).toBe(moment(date).format('YYYY'))
})
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/test/constructor.test.js | test/constructor.test.js | import MockDate from 'mockdate'
import dayjs from '../src'
beforeEach(() => {
MockDate.set(new Date())
})
afterEach(() => {
MockDate.reset()
})
it('supports instanceof dayjs', () => {
expect(dayjs() instanceof dayjs).toBeTruthy()
})
it('$isDayjsObject', () => {
const mockOtherVersionDayjsObj = {
$isDayjsObject: true
}
expect(dayjs.isDayjs(mockOtherVersionDayjsObj)).toBeTruthy()
})
it('does not break isDayjs', () => {
expect(dayjs.isDayjs(dayjs())).toBeTruthy()
expect(dayjs.isDayjs(new Date())).toBeFalsy()
})
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/test/issues/issue2027.swapped-order.test.js | test/issues/issue2027.swapped-order.test.js | import MockDate from 'mockdate'
import dayjs from '../../src'
import duration from '../../src/plugin/duration'
import objectSupport from '../../src/plugin/objectSupport'
dayjs.extend(duration)
dayjs.extend(objectSupport)
beforeEach(() => {
MockDate.set(new Date())
})
afterEach(() => {
MockDate.reset()
})
// issue 2027
describe('issue 2027 - order objectSupport > Duration', () => {
it('add Duration object returns correct date', () => {
const baseDate = dayjs('2022-06-26T14:01:02.003')
const durationToAdd = dayjs.duration(6, 'hours')
const testDate = baseDate.add(durationToAdd)
expect(testDate.format('YYYY-MM-DD HH:mm:ss.SSS')).toBe('2022-06-26 20:01:02.003')
})
it('subtract Duration object returns correct date', () => {
const baseDate = dayjs('2022-06-26T14:01:02.003')
const durationToAdd = dayjs.duration(6, 'hours')
const testDate = baseDate.subtract(durationToAdd)
expect(testDate.format('YYYY-MM-DD HH:mm:ss.SSS')).toBe('2022-06-26 08:01:02.003')
})
it('add number with unit returns correct date', () => {
const baseDate = dayjs('2022-06-26T14:01:02.003')
const testDate = baseDate.add(6, 'hours')
expect(testDate.format('YYYY-MM-DD HH:mm:ss.SSS')).toBe('2022-06-26 20:01:02.003')
})
it('subtract number with unit returns correct date', () => {
const baseDate = dayjs('2022-06-26T14:01:02.003')
const testDate = baseDate.subtract(6, 'hours')
expect(testDate.format('YYYY-MM-DD HH:mm:ss.SSS')).toBe('2022-06-26 08:01:02.003')
})
it('parse string returns correct date', () => {
const testDate = dayjs('2022-06-26T14:01:02.003')
expect(testDate.format('YYYY-MM-DD HH:mm:ss.SSS')).toBe('2022-06-26 14:01:02.003')
})
it('parse object returns correct date', () => {
const testDate = dayjs({
year: '2022',
month: '05',
day: '26',
hour: '14',
minute: '01',
second: '02',
millisecond: '003'
})
expect(testDate.format('YYYY-MM-DD HH:mm:ss.SSS')).toBe('2022-06-26 14:01:02.003')
})
it('set hour with number returns correct date', () => {
const baseDate = dayjs('2022-06-26T14:01:02.003')
const testDate = baseDate.hour(10)
expect(testDate.format('YYYY-MM-DD HH:mm:ss.SSS')).toBe('2022-06-26 10:01:02.003')
})
it('set hour with object returns correct date', () => {
const baseDate = dayjs('2022-06-26T14:01:02.003')
const testDate = baseDate.set({ hour: '10' })
expect(testDate.format('YYYY-MM-DD HH:mm:ss.SSS')).toBe('2022-06-26 10:01:02.003')
})
})
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/test/issues/issue2027.correct-order.test.js | test/issues/issue2027.correct-order.test.js | import MockDate from 'mockdate'
import dayjs from '../../src'
import duration from '../../src/plugin/duration'
import objectSupport from '../../src/plugin/objectSupport'
dayjs.extend(objectSupport)
dayjs.extend(duration)
beforeEach(() => {
MockDate.set(new Date())
})
afterEach(() => {
MockDate.reset()
})
// issue 2027
describe('issue 2027 - order objectSupport > Duration', () => {
it('add Duration object returns correct date', () => {
const baseDate = dayjs('2022-06-26T14:01:02.003')
const durationToAdd = dayjs.duration(6, 'hours')
const testDate = baseDate.add(durationToAdd)
expect(testDate.format('YYYY-MM-DD HH:mm:ss.SSS')).toBe('2022-06-26 20:01:02.003')
})
it('subtract Duration object returns correct date', () => {
const baseDate = dayjs('2022-06-26T14:01:02.003')
const durationToAdd = dayjs.duration(6, 'hours')
const testDate = baseDate.subtract(durationToAdd)
expect(testDate.format('YYYY-MM-DD HH:mm:ss.SSS')).toBe('2022-06-26 08:01:02.003')
})
it('add number with unit returns correct date', () => {
const baseDate = dayjs('2022-06-26T14:01:02.003')
const testDate = baseDate.add(6, 'hours')
expect(testDate.format('YYYY-MM-DD HH:mm:ss.SSS')).toBe('2022-06-26 20:01:02.003')
})
it('subtract number with unit returns correct date', () => {
const baseDate = dayjs('2022-06-26T14:01:02.003')
const testDate = baseDate.subtract(6, 'hours')
expect(testDate.format('YYYY-MM-DD HH:mm:ss.SSS')).toBe('2022-06-26 08:01:02.003')
})
it('parse string returns correct date', () => {
const testDate = dayjs('2022-06-26T14:01:02.003')
expect(testDate.format('YYYY-MM-DD HH:mm:ss.SSS')).toBe('2022-06-26 14:01:02.003')
})
it('parse object returns correct date', () => {
const testDate = dayjs({
year: '2022',
month: '05',
day: '26',
hour: '14',
minute: '01',
second: '02',
millisecond: '003'
})
expect(testDate.format('YYYY-MM-DD HH:mm:ss.SSS')).toBe('2022-06-26 14:01:02.003')
})
it('set hour with number returns correct date', () => {
const baseDate = dayjs('2022-06-26T14:01:02.003')
const testDate = baseDate.hour(10)
expect(testDate.format('YYYY-MM-DD HH:mm:ss.SSS')).toBe('2022-06-26 10:01:02.003')
})
it('set hour with object returns correct date', () => {
const baseDate = dayjs('2022-06-26T14:01:02.003')
const testDate = baseDate.set({ hour: '10' })
expect(testDate.format('YYYY-MM-DD HH:mm:ss.SSS')).toBe('2022-06-26 10:01:02.003')
})
})
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/test/plugin/timezone.test.js | test/plugin/timezone.test.js | import MockDate from 'mockdate'
import moment from 'moment-timezone'
import dayjs from '../../src'
import timezone from '../../src/plugin/timezone'
import customParseFormat from '../../src/plugin/customParseFormat'
import utc from '../../src/plugin/utc'
dayjs.extend(utc)
dayjs.extend(timezone)
dayjs.extend(customParseFormat)
beforeEach(() => {
MockDate.set(new Date())
})
afterEach(() => {
MockDate.reset()
})
const NY = 'America/New_York'
const VAN = 'America/Vancouver'
const DEN = 'America/Denver'
const TOKYO = 'Asia/Tokyo'
const PARIS = 'Europe/Paris'
describe('Guess', () => {
it('return string', () => {
expect(typeof dayjs.tz.guess()).toBe('string')
})
})
describe('Parse', () => {
it('parse target time string', () => {
const newYork = dayjs.tz('2014-06-01 12:00', NY)
const MnewYork = moment.tz('2014-06-01 12:00', NY)
expect(newYork.format()).toBe('2014-06-01T12:00:00-04:00')
expect(newYork.format()).toBe(MnewYork.format())
expect(newYork.utcOffset()).toBe(-240)
expect(newYork.utcOffset()).toBe(MnewYork.utcOffset())
expect(newYork.valueOf()).toBe(1401638400000)
expect(newYork.valueOf()).toBe(MnewYork.valueOf())
})
it('parse timestamp, js Date, Day.js object', () => {
const d = new Date('2020-08-07T12:00-07:00')
const result = '2020-08-07T12:00:00-07:00'
const TjsDate = dayjs.tz(d, VAN)
const Tdayjs = dayjs.tz(dayjs(d), VAN)
const Timestamp = dayjs.tz(d.getTime(), VAN)
const Tmoment = moment.tz(d, VAN)
expect(TjsDate.format()).toBe(result)
expect(Tdayjs.format()).toBe(result)
expect(Timestamp.format()).toBe(result)
expect(Tmoment.format()).toBe(result)
})
it('parse and convert between timezones', () => {
const newYork = dayjs.tz('2014-06-01 12:00', NY)
expect(newYork.tz('America/Los_Angeles').format()).toBe('2014-06-01T09:00:00-07:00')
expect(newYork.tz('Europe/London').format()).toBe('2014-06-01T17:00:00+01:00')
})
it('preserve milliseconds', () => {
const d = dayjs(1596735327399)
const oldMs = d.millisecond()
const dTz = d.tz('America/New_York')
const newMs = dTz.millisecond()
expect(oldMs).toEqual(newMs)
})
})
describe('Convert', () => {
it('convert to target time', () => {
const losAngeles = dayjs('2014-06-01T12:00:00Z').tz('America/Los_Angeles')
const MlosAngeles = moment('2014-06-01T12:00:00Z').tz('America/Los_Angeles')
expect(losAngeles.format()).toBe('2014-06-01T05:00:00-07:00')
expect(losAngeles.format()).toBe(MlosAngeles.format())
expect(losAngeles.valueOf()).toBe(1401624000000)
expect(losAngeles.valueOf()).toBe(MlosAngeles.valueOf())
expect(losAngeles.utcOffset()).toBe(-420)
expect(losAngeles.utcOffset()).toBe(MlosAngeles.utcOffset())
})
it('convert to target time', () => {
[dayjs, moment].forEach((_) => {
const losAngeles = _('2014-06-01T12:00:00Z').tz('America/Los_Angeles')
expect(losAngeles.format()).toBe('2014-06-01T05:00:00-07:00')
expect(losAngeles.valueOf()).toBe(1401624000000)
})
})
it('convert from time with timezone to target time', () => {
const losAngelesInUTC = dayjs('2014-06-01T05:00:00-07:00').tz('UTC')
const MlosAngelesInUTC = moment('2014-06-01T05:00:00-07:00').tz('UTC')
expect(losAngelesInUTC.format()).toBe('2014-06-01T12:00:00Z')
expect(losAngelesInUTC.format()).toBe(MlosAngelesInUTC.format())
})
it('DST', () => {
[dayjs, moment].forEach((_) => {
const jun = _('2014-06-01T12:00:00Z')
const dec = _('2014-12-01T12:00:00Z')
expect(jun.tz('America/Los_Angeles').format('ha')).toBe('5am')
expect(jun.tz('America/Los_Angeles').utcOffset()).toBe(-7 * 60)
expect(dec.tz('America/Los_Angeles').format('ha')).toBe('4am')
expect(dec.tz('America/Los_Angeles').utcOffset()).toBe(-8 * 60)
expect(jun.tz(NY).format('ha')).toBe('8am')
expect(dec.tz(NY).format('ha')).toBe('7am')
expect(jun.tz(TOKYO).format('ha')).toBe('9pm')
expect(dec.tz(TOKYO).format('ha')).toBe('9pm')
expect(jun.tz('Australia/Sydney').format('ha')).toBe('10pm')
expect(dec.tz('Australia/Sydney').format('ha')).toBe('11pm')
})
})
it('format Z', () => {
[dayjs, moment].forEach((_) => {
const t = _('2020-08-06T03:48:10.258Z').tz(TOKYO)
expect(t.format('Z')).toBe('+09:00')
})
})
})
describe('DST, a time that never existed Spring Forward', () => {
// 11 March 2012, 02:00:00 clocks were
// turned forward 1 hour to 11 March 2012, 03:00:00 local
// daylight time instead.
// 02:00 -> 03:00
// 02:59 -> 03:59
it('2012-03-11 01:59:59', () => {
const s = '2012-03-11 01:59:59'
const d = dayjs.tz(s, NY)
const m = moment.tz(s, NY)
expect(d.format()).toBe('2012-03-11T01:59:59-05:00')
expect(d.format()).toBe(m.format())
expect(d.utcOffset()).toBe(-300)
expect(d.utcOffset()).toBe(m.utcOffset())
expect(d.valueOf()).toBe(1331449199000)
expect(d.valueOf()).toBe(m.valueOf())
})
it('2012-03-11 02:00:00', () => {
const s = '2012-03-11 02:00:00'
const d = dayjs.tz(s, NY)
const m = moment.tz(s, NY)
expect(d.format()).toBe('2012-03-11T03:00:00-04:00')
expect(d.format()).toBe(m.format())
expect(d.valueOf()).toBe(m.valueOf())
expect(d.valueOf()).toBe(1331449200000)
expect(d.utcOffset()).toBe(-240)
expect(d.utcOffset()).toBe(m.utcOffset())
})
it('2012-03-11 02:59:59', () => {
const s = '2012-03-11 02:59:59'
const d = dayjs.tz(s, NY)
const m = moment.tz(s, NY)
expect(d.format()).toBe('2012-03-11T03:59:59-04:00')
expect(d.format()).toBe(m.format())
expect(d.valueOf()).toBe(m.valueOf())
expect(d.valueOf()).toBe(1331452799000)
expect(d.utcOffset()).toBe(-240)
expect(d.utcOffset()).toBe(m.utcOffset())
})
it('2012-03-11 03:00:00', () => {
const s = '2012-03-11 03:00:00'
const d = dayjs.tz(s, NY)
const m = moment.tz(s, NY)
expect(d.format()).toBe('2012-03-11T03:00:00-04:00')
expect(d.format()).toBe(m.format())
expect(d.valueOf()).toBe(m.valueOf())
expect(d.valueOf()).toBe(1331449200000)
expect(d.utcOffset()).toBe(-240)
expect(d.utcOffset()).toBe(m.utcOffset())
})
})
describe('DST, a time that never existed Fall Back', () => {
// In the fall, at the end of DST
it('2012-11-04 00:59:59', () => {
const s = '2012-11-04 00:59:59';
[dayjs, moment].forEach((_) => {
const d = _.tz(s, NY)
expect(d.format()).toBe('2012-11-04T00:59:59-04:00')
expect(d.utcOffset()).toBe(-240)
expect(d.valueOf()).toBe(1352005199000)
})
})
it('2012-11-04 00:59:59', () => {
const s = '2012-11-04 00:59:59';
[dayjs, moment].forEach((_) => {
const d = _.tz(s, NY)
expect(d.format()).toBe('2012-11-04T00:59:59-04:00')
expect(d.utcOffset()).toBe(-240)
expect(d.valueOf()).toBe(1352005199000)
})
})
// there's no sense to test "2012-11-04 01:59:59 America/New_York"
// cause it's an invalid date and never exist
// and dayjs result it as "2012-11-04T01:59:00-05:00"
it('2012-11-04 02:00:00', () => {
const s = '2012-11-04 02:00:00';
[dayjs, moment].forEach((_) => {
const d = _.tz(s, NY)
expect(d.format()).toBe('2012-11-04T02:00:00-05:00')
expect(d.utcOffset()).toBe(-300)
expect(d.valueOf()).toBe(1352012400000)
})
})
})
it('DST valueOf', () => {
const day1 = '2021-11-17T09:45:00.000Z'
const d1 = dayjs.utc(day1).tz(PARIS)
const m1 = moment.tz(day1, PARIS)
expect(d1.valueOf()).toBe(m1.valueOf())
const day2 = '2021-05-17T09:45:00.000Z'
const d2 = dayjs.utc(day2).tz(PARIS)
const m2 = moment.tz(day2, PARIS)
expect(d2.valueOf()).toBe(m2.valueOf())
})
describe('set Default', () => {
it('default timezone', () => {
const dateStr = '2014-06-01 12:00'
dayjs.tz.setDefault(NY)
const newYork = dayjs.tz(dateStr)
expect(newYork.format()).toBe('2014-06-01T12:00:00-04:00')
expect(newYork.utcOffset()).toBe(-240)
expect(newYork.valueOf()).toBe(1401638400000)
expect(dayjs(dateStr).tz().format()).toBe(dayjs(dateStr).tz(NY).format())
})
it('empty timezone means local timezone', () => {
const LOCAL_TZ = dayjs.tz.guess()
const dateStr = '2014-06-01 12:00'
dayjs.tz.setDefault()
expect(dayjs(dateStr).tz().valueOf()).toBe(dayjs(dateStr).tz(LOCAL_TZ).valueOf())
expect(dayjs.tz(dateStr).valueOf()).toBe(dayjs.tz(dateStr, LOCAL_TZ).valueOf())
})
it('change default timezone', () => {
dayjs.tz.setDefault(NY)
const newYork = dayjs.tz('2014-06-01 12:00')
expect(newYork.utcOffset()).toBe(-240)
dayjs.tz.setDefault(TOKYO)
const tokyo = dayjs.tz('2014-06-01 12:00')
expect(tokyo.format()).toBe('2014-06-01T12:00:00+09:00')
expect(tokyo.format('Z')).toBe('+09:00')
expect(tokyo.valueOf()).toBe(1401591600000)
})
it('override default timezone in proto.tz', () => {
dayjs.tz.setDefault(NY)
const tokyo = dayjs.tz('2014-06-01 12:00', TOKYO)
expect(tokyo.format()).toBe('2014-06-01T12:00:00+09:00')
expect(tokyo.format('Z')).toBe('+09:00')
expect(tokyo.valueOf()).toBe(1401591600000)
})
it('override default timezone in d.tz', () => {
dayjs.tz.setDefault(NY)
const tokyo = dayjs.tz('2014-06-01 12:00', TOKYO)
expect(tokyo.format()).toBe('2014-06-01T12:00:00+09:00')
expect(tokyo.format('Z')).toBe('+09:00')
expect(tokyo.valueOf()).toBe(1401591600000)
})
})
describe('keepLocalTime', () => {
const base = dayjs.tz('2013-11-18 11:55', 'America/Toronto')
it('keepLocalTime', () => {
expect(base.tz('Europe/Berlin').format()).toBe('2013-11-18T17:55:00+01:00')
expect(base.tz('Europe/Berlin', true).format()).toBe('2013-11-18T11:55:00+01:00')
})
})
describe('Get offsetName', () => {
const dtz = dayjs.tz('2012-03-11 01:59:59', NY)
it('short', () => {
const d = dtz.offsetName('short')
const m = moment.tz('2012-03-11 01:59:59', NY).format('z')
expect(d).toBe(m)
expect(d).toBe('EST')
})
it('long', () => {
const d = dtz.offsetName('long')
expect(d).toBe('Eastern Standard Time')
})
})
describe('CustomPraseFormat', () => {
const result = 1602786600
it('normal', () => {
expect(dayjs.tz('2020/10/15 12:30', DEN).unix()).toBe(result)
})
it('custom', () => {
expect(dayjs.tz('10/15/2020 12:30', 'MM/DD/YYYY HH:mm', DEN).unix()).toBe(result)
})
})
describe('startOf and endOf', () => {
it('corrects for timezone offset in startOf', () => {
const originalDay = dayjs.tz('2010-01-01 00:00:00', NY)
const startOfDay = originalDay.startOf('day')
expect(startOfDay.valueOf()).toEqual(originalDay.valueOf())
})
it('corrects for timezone offset in endOf', () => {
const originalDay = dayjs.tz('2009-12-31 23:59:59.999', NY)
const endOfDay = originalDay.endOf('day')
expect(endOfDay.valueOf()).toEqual(originalDay.valueOf())
})
it('preserves locality when tz is called', () => {
const tzWithoutLocality = dayjs.tz('2023-02-17 00:00:00', NY)
const tzWithLocality = dayjs.tz('2023-02-17 00:00:00', NY).locale({
name: 'locale_test',
weekStart: 3
})
expect(tzWithoutLocality.startOf('week').format('YYYY-MM-DD')).toEqual('2023-02-12')
expect(tzWithLocality.startOf('week').format('YYYY-MM-DD')).toEqual('2023-02-15')
})
})
describe('UTC timezone', () => {
it('TZ with UTC with Locale', () => {
const test1 = dayjs('2000-01-01T09:00:00+09:00').tz('Asia/Seoul').locale('en')
expect(test1.hour()).toBe(9)
const test2 = dayjs('2000-01-01T09:00:00+09:00').tz('Asia/Hong_Kong').locale('en')
expect(test2.hour()).toBe(8)
const test3 = dayjs('2000-01-01T09:00:00+09:00').tz('Etc/UTC').locale('en')
expect(test3.hour()).toBe(0)
})
it('TZ with UTC', () => {
const dayjs1 = dayjs('2000-01-01T09:01:00+09:00').tz('Etc/UTC', false)
expect(dayjs1.format()).toBe('2000-01-01T00:01:00Z')
const moment1 = moment('2000-01-01T09:01:00+09:00').tz('Etc/UTC', false)
expect(moment1.format()).toBe('2000-01-01T00:01:00Z')
const dayjs2 = dayjs('2000-01-01T09:01:00+09:00').tz('Etc/UTC', true)
const moment2 = moment('2000-01-01T09:01:00+09:00').tz('Etc/UTC', true)
expect(dayjs2.format()).toBe(moment2.format())
})
})
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/test/plugin/isSameOrAfter.test.js | test/plugin/isSameOrAfter.test.js | import MockDate from 'mockdate'
import dayjs from '../../src'
import isSameOrAfter from '../../src/plugin/isSameOrAfter'
dayjs.extend(isSameOrAfter)
beforeEach(() => {
MockDate.set(new Date())
})
afterEach(() => {
MockDate.reset()
})
test('is same or after year', () => {
const m = dayjs(new Date(2011, 1, 2, 3, 4, 5, 6))
const mCopy = dayjs(m)
expect(m.isSameOrAfter(dayjs(new Date(2011, 5, 6, 7, 8, 9, 10)), 'year')).toBe(true, 'year match')
expect(m.isSameOrAfter(dayjs(new Date(2011, 5, 6, 7, 8, 9, 10)), 'years')).toBe(true, 'plural should work')
expect(m.isSameOrAfter(dayjs(new Date(2012, 5, 6, 7, 8, 9, 10)), 'year')).toBe(false, 'year is later')
expect(m.isSameOrAfter(dayjs(new Date(2010, 5, 6, 7, 8, 9, 10)), 'year')).toBe(true, 'year is earlier')
expect(m.isSameOrAfter(dayjs(new Date(2011, 0, 1, 0, 0, 0, 0)), 'year')).toBe(true, 'exact start of year')
expect(m.isSameOrAfter(dayjs(new Date(2011, 11, 31, 23, 59, 59, 999)), 'year')).toBe(true, 'exact end of year')
expect(m.isSameOrAfter(dayjs(new Date(2012, 0, 1, 0, 0, 0, 0)), 'year')).toBe(false, 'start of next year')
expect(m.isSameOrAfter(dayjs(new Date(2010, 11, 31, 23, 59, 59, 999)), 'year')).toBe(true, 'end of previous year')
expect(m.isSameOrAfter(m, 'year')).toBe(true, 'same moments are in the same year')
expect(+m).toEqual(+mCopy, 'isSameOrAfter year should not change moment')
})
test('is same or after without units', () => {
const m = dayjs(new Date(2011, 3, 2, 3, 4, 5, 10))
const mCopy = dayjs(m)
expect(m.isSameOrAfter(dayjs(new Date(2012, 3, 2, 3, 5, 5, 10)))).toBe(false, 'year is later')
expect(m.isSameOrAfter(dayjs(new Date(2010, 3, 2, 3, 3, 5, 10)))).toBe(true, 'year is earlier')
expect(m.isSameOrAfter(dayjs(new Date(2011, 4, 2, 3, 4, 5, 10)))).toBe(false, 'month is later')
expect(m.isSameOrAfter(dayjs(new Date(2011, 2, 2, 3, 4, 5, 10)))).toBe(true, 'month is earlier')
expect(m.isSameOrAfter(dayjs(new Date(2011, 3, 3, 3, 4, 5, 10)))).toBe(false, 'day is later')
expect(m.isSameOrAfter(dayjs(new Date(2011, 3, 1, 3, 4, 5, 10)))).toBe(true, 'day is earlier')
expect(m.isSameOrAfter(dayjs(new Date(2011, 3, 2, 4, 4, 5, 10)))).toBe(false, 'hour is later')
expect(m.isSameOrAfter(dayjs(new Date(2011, 3, 2, 2, 4, 5, 10)))).toBe(true, 'hour is earlier')
expect(m.isSameOrAfter(dayjs(new Date(2011, 3, 2, 3, 5, 5, 10)))).toBe(false, 'minute is later')
expect(m.isSameOrAfter(dayjs(new Date(2011, 3, 2, 3, 3, 5, 10)))).toBe(true, 'minute is earlier')
expect(m.isSameOrAfter(dayjs(new Date(2011, 3, 2, 3, 4, 6, 10)))).toBe(false, 'second is later')
expect(m.isSameOrAfter(dayjs(new Date(2011, 3, 2, 3, 4, 4, 11)))).toBe(true, 'second is earlier')
expect(m.isSameOrAfter(dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)))).toBe(true, 'millisecond match')
expect(m.isSameOrAfter(dayjs(new Date(2011, 3, 2, 3, 4, 5, 11)))).toBe(false, 'millisecond is later')
expect(m.isSameOrAfter(dayjs(new Date(2011, 3, 2, 3, 4, 5, 9)))).toBe(true, 'millisecond is earlier')
expect(m.isSameOrAfter(m)).toBe(true, 'moments are the same as themselves')
expect(+m).toEqual(+mCopy, 'isSameOrAfter second should not change moment')
})
test('is same or after without date', () => {
const past = dayjs().subtract(1, 'day')
const future = dayjs().add(1, 'day')
expect(past.isSameOrAfter()).toBe(false, 'past is before now')
expect(future.isSameOrAfter()).toBe(true, 'future is not before now')
})
test('is same or after month', () => {
const m = dayjs(new Date(2011, 2, 3, 4, 5, 6, 7))
const mCopy = dayjs(m)
expect(m.isSameOrAfter(dayjs(new Date(2011, 2, 6, 7, 8, 9, 10)), 'month')).toBe(true, 'month match')
expect(m.isSameOrAfter(dayjs(new Date(2011, 2, 6, 7, 8, 9, 10)), 'months')).toBe(true, 'plural should work')
expect(m.isSameOrAfter(dayjs(new Date(2012, 2, 6, 7, 8, 9, 10)), 'month')).toBe(false, 'year is later')
expect(m.isSameOrAfter(dayjs(new Date(2010, 2, 6, 7, 8, 9, 10)), 'month')).toBe(true, 'year is earlier')
expect(m.isSameOrAfter(dayjs(new Date(2011, 5, 6, 7, 8, 9, 10)), 'month')).toBe(false, 'month is later')
expect(m.isSameOrAfter(dayjs(new Date(2011, 1, 6, 7, 8, 9, 10)), 'month')).toBe(true, 'month is earlier')
expect(m.isSameOrAfter(dayjs(new Date(2011, 2, 1, 0, 0, 0, 0)), 'month')).toBe(true, 'exact start of month')
expect(m.isSameOrAfter(dayjs(new Date(2011, 2, 31, 23, 59, 59, 999)), 'month')).toBe(true, 'exact end of month')
expect(m.isSameOrAfter(dayjs(new Date(2011, 3, 1, 0, 0, 0, 0)), 'month')).toBe(false, 'start of next month')
expect(m.isSameOrAfter(dayjs(new Date(2011, 1, 27, 23, 59, 59, 999)), 'month')).toBe(true, 'end of previous month')
expect(m.isSameOrAfter(m, 'month')).toBe(true, 'same moments are in the same month')
expect(+m).toEqual(+mCopy, 'isSameOrAfter month should not change moment')
})
test('is same or after day', () => {
const m = dayjs(new Date(2011, 1, 2, 3, 4, 5, 6))
const mCopy = dayjs(m)
expect(m.isSameOrAfter(dayjs(new Date(2011, 1, 2, 7, 8, 9, 10)), 'day')).toBe(true, 'day match')
expect(m.isSameOrAfter(dayjs(new Date(2011, 1, 2, 7, 8, 9, 10)), 'days')).toBe(true, 'plural should work')
expect(m.isSameOrAfter(dayjs(new Date(2012, 1, 2, 7, 8, 9, 10)), 'day')).toBe(false, 'year is later')
expect(m.isSameOrAfter(dayjs(new Date(2010, 1, 2, 7, 8, 9, 10)), 'day')).toBe(true, 'year is earlier')
expect(m.isSameOrAfter(dayjs(new Date(2011, 2, 2, 7, 8, 9, 10)), 'day')).toBe(false, 'month is later')
expect(m.isSameOrAfter(dayjs(new Date(2010, 12, 2, 7, 8, 9, 10)), 'day')).toBe(true, 'month is earlier')
expect(m.isSameOrAfter(dayjs(new Date(2011, 1, 3, 7, 8, 9, 10)), 'day')).toBe(false, 'day is later')
expect(m.isSameOrAfter(dayjs(new Date(2011, 1, 1, 7, 8, 9, 10)), 'day')).toBe(true, 'day is earlier')
expect(m.isSameOrAfter(dayjs(new Date(2011, 1, 2, 0, 0, 0, 0)), 'day')).toBe(true, 'exact start of day')
expect(m.isSameOrAfter(dayjs(new Date(2011, 1, 2, 23, 59, 59, 999)), 'day')).toBe(true, 'exact end of day')
expect(m.isSameOrAfter(dayjs(new Date(2011, 1, 3, 0, 0, 0, 0)), 'day')).toBe(false, 'start of next day')
expect(m.isSameOrAfter(dayjs(new Date(2011, 1, 1, 23, 59, 59, 999)), 'day')).toBe(true, 'end of previous day')
expect(m.isSameOrAfter(m, 'day')).toBe(true, 'same moments are in the same day')
expect(+m).toEqual(+mCopy, 'isSameOrAfter day should not change moment')
})
test('is same or after hour', () => {
const m = dayjs(new Date(2011, 1, 2, 3, 4, 5, 6))
const mCopy = dayjs(m)
expect(m.isSameOrAfter(dayjs(new Date(2011, 1, 2, 3, 8, 9, 10)), 'hour')).toBe(true, 'hour match')
expect(m.isSameOrAfter(dayjs(new Date(2011, 1, 2, 3, 8, 9, 10)), 'hours')).toBe(true, 'plural should work')
expect(m.isSameOrAfter(dayjs(new Date(2012, 1, 2, 3, 8, 9, 10)), 'hour')).toBe(false, 'year is later')
expect(m.isSameOrAfter(dayjs(new Date(2010, 1, 2, 3, 8, 9, 10)), 'hour')).toBe(true, 'year is earlier')
expect(m.isSameOrAfter(dayjs(new Date(2011, 2, 2, 3, 8, 9, 10)), 'hour')).toBe(false, 'month is later')
expect(m.isSameOrAfter(dayjs(new Date(2010, 12, 2, 3, 8, 9, 10)), 'hour')).toBe(true, 'month is earlier')
expect(m.isSameOrAfter(dayjs(new Date(2011, 1, 3, 3, 8, 9, 10)), 'hour')).toBe(false, 'day is later')
expect(m.isSameOrAfter(dayjs(new Date(2011, 1, 1, 3, 8, 9, 10)), 'hour')).toBe(true, 'day is earlier')
expect(m.isSameOrAfter(dayjs(new Date(2011, 1, 2, 4, 8, 9, 10)), 'hour')).toBe(false, 'hour is later')
expect(m.isSameOrAfter(dayjs(new Date(2011, 1, 2, 2, 8, 9, 10)), 'hour')).toBe(true, 'hour is earlier')
expect(m.isSameOrAfter(dayjs(new Date(2011, 1, 2, 3, 0, 0, 0)), 'hour')).toBe(true, 'exact start of hour')
expect(m.isSameOrAfter(dayjs(new Date(2011, 1, 2, 3, 59, 59, 999)), 'hour')).toBe(true, 'exact end of hour')
expect(m.isSameOrAfter(dayjs(new Date(2011, 1, 2, 4, 0, 0, 0)), 'hour')).toBe(false, 'start of next hour')
expect(m.isSameOrAfter(dayjs(new Date(2011, 1, 2, 2, 59, 59, 999)), 'hour')).toBe(true, 'end of previous hour')
expect(m.isSameOrAfter(m, 'hour')).toBe(true, 'same moments are in the same hour')
expect(+m).toEqual(+mCopy, 'isSameOrAfter hour should not change moment')
})
test('is same or after minute', () => {
const m = dayjs(new Date(2011, 1, 2, 3, 4, 5, 6))
const mCopy = dayjs(m)
expect(m.isSameOrAfter(dayjs(new Date(2011, 1, 2, 3, 4, 9, 10)), 'minute')).toBe(true, 'minute match')
expect(m.isSameOrAfter(dayjs(new Date(2011, 1, 2, 3, 4, 9, 10)), 'minutes')).toBe(true, 'plural should work')
expect(m.isSameOrAfter(dayjs(new Date(2012, 1, 2, 3, 4, 9, 10)), 'minute')).toBe(false, 'year is later')
expect(m.isSameOrAfter(dayjs(new Date(2010, 1, 2, 3, 4, 9, 10)), 'minute')).toBe(true, 'year is earlier')
expect(m.isSameOrAfter(dayjs(new Date(2011, 2, 2, 3, 4, 9, 10)), 'minute')).toBe(false, 'month is later')
expect(m.isSameOrAfter(dayjs(new Date(2010, 12, 2, 3, 4, 9, 10)), 'minute')).toBe(true, 'month is earlier')
expect(m.isSameOrAfter(dayjs(new Date(2011, 1, 3, 3, 4, 9, 10)), 'minute')).toBe(false, 'day is later')
expect(m.isSameOrAfter(dayjs(new Date(2011, 1, 1, 3, 4, 9, 10)), 'minute')).toBe(true, 'day is earlier')
expect(m.isSameOrAfter(dayjs(new Date(2011, 1, 2, 4, 4, 9, 10)), 'minute')).toBe(false, 'hour is later')
expect(m.isSameOrAfter(dayjs(new Date(2011, 1, 2, 2, 4, 9, 10)), 'minute')).toBe(true, 'hour is earlier')
expect(m.isSameOrAfter(dayjs(new Date(2011, 1, 2, 3, 5, 9, 10)), 'minute')).toBe(false, 'minute is later')
expect(m.isSameOrAfter(dayjs(new Date(2011, 1, 2, 3, 3, 9, 10)), 'minute')).toBe(true, 'minute is earlier')
expect(m.isSameOrAfter(dayjs(new Date(2011, 1, 2, 3, 4, 0, 0)), 'minute')).toBe(true, 'exact start of minute')
expect(m.isSameOrAfter(dayjs(new Date(2011, 1, 2, 3, 4, 59, 999)), 'minute')).toBe(true, 'exact end of minute')
expect(m.isSameOrAfter(dayjs(new Date(2011, 1, 2, 3, 5, 0, 0)), 'minute')).toBe(false, 'start of next minute')
expect(m.isSameOrAfter(dayjs(new Date(2011, 1, 2, 3, 3, 59, 999)), 'minute')).toBe(true, 'end of previous minute')
expect(m.isSameOrAfter(m, 'minute')).toBe(true, 'same moments are in the same minute')
expect(+m).toEqual(+mCopy, 'isSameOrAfter minute should not change moment')
})
test('is same or after second', () => {
const m = dayjs(new Date(2011, 1, 2, 3, 4, 5, 6))
const mCopy = dayjs(m)
expect(m.isSameOrAfter(dayjs(new Date(2011, 1, 2, 3, 4, 5, 10)), 'second')).toBe(true, 'second match')
expect(m.isSameOrAfter(dayjs(new Date(2011, 1, 2, 3, 4, 5, 10)), 'seconds')).toBe(true, 'plural should work')
expect(m.isSameOrAfter(dayjs(new Date(2012, 1, 2, 3, 4, 5, 10)), 'second')).toBe(false, 'year is later')
expect(m.isSameOrAfter(dayjs(new Date(2010, 1, 2, 3, 4, 5, 10)), 'second')).toBe(true, 'year is earlier')
expect(m.isSameOrAfter(dayjs(new Date(2011, 2, 2, 3, 4, 5, 10)), 'second')).toBe(false, 'month is later')
expect(m.isSameOrAfter(dayjs(new Date(2010, 12, 2, 3, 4, 5, 10)), 'second')).toBe(true, 'month is earlier')
expect(m.isSameOrAfter(dayjs(new Date(2011, 1, 3, 3, 4, 5, 10)), 'second')).toBe(false, 'day is later')
expect(m.isSameOrAfter(dayjs(new Date(2011, 1, 1, 3, 4, 5, 10)), 'second')).toBe(true, 'day is earlier')
expect(m.isSameOrAfter(dayjs(new Date(2011, 1, 2, 4, 4, 5, 10)), 'second')).toBe(false, 'hour is later')
expect(m.isSameOrAfter(dayjs(new Date(2011, 1, 2, 2, 4, 5, 10)), 'second')).toBe(true, 'hour is earlier')
expect(m.isSameOrAfter(dayjs(new Date(2011, 1, 2, 3, 5, 5, 10)), 'second')).toBe(false, 'minute is later')
expect(m.isSameOrAfter(dayjs(new Date(2011, 1, 2, 3, 3, 5, 10)), 'second')).toBe(true, 'minute is earlier')
expect(m.isSameOrAfter(dayjs(new Date(2011, 1, 2, 3, 4, 6, 10)), 'second')).toBe(false, 'second is later')
expect(m.isSameOrAfter(dayjs(new Date(2011, 1, 2, 3, 4, 4, 10)), 'second')).toBe(true, 'second is earlier')
expect(m.isSameOrAfter(dayjs(new Date(2011, 1, 2, 3, 4, 5, 0)), 'second')).toBe(true, 'exact start of second')
expect(m.isSameOrAfter(dayjs(new Date(2011, 1, 2, 3, 4, 5, 999)), 'second')).toBe(true, 'exact end of second')
expect(m.isSameOrAfter(dayjs(new Date(2011, 1, 2, 3, 4, 6, 0)), 'second')).toBe(false, 'start of next second')
expect(m.isSameOrAfter(dayjs(new Date(2011, 1, 2, 3, 4, 4, 999)), 'second')).toBe(true, 'end of previous second')
expect(m.isSameOrAfter(m, 'second')).toBe(true, 'same moments are in the same second')
expect(+m).toEqual(+mCopy, 'isSameOrAfter second should not change moment')
})
test('is same or after millisecond', () => {
const m = dayjs(new Date(2011, 3, 2, 3, 4, 5, 10))
const mCopy = dayjs(m)
expect(m.isSameOrAfter(dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)), 'millisecond')).toBe(true, 'millisecond match')
expect(m.isSameOrAfter(dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)), 'milliseconds')).toBe(true, 'plural should work')
expect(m.isSameOrAfter(dayjs(new Date(2012, 3, 2, 3, 4, 5, 10)), 'millisecond')).toBe(false, 'year is later')
expect(m.isSameOrAfter(dayjs(new Date(2010, 3, 2, 3, 4, 5, 10)), 'millisecond')).toBe(true, 'year is earlier')
expect(m.isSameOrAfter(dayjs(new Date(2011, 4, 2, 3, 4, 5, 10)), 'millisecond')).toBe(false, 'month is later')
expect(m.isSameOrAfter(dayjs(new Date(2011, 2, 2, 3, 4, 5, 10)), 'millisecond')).toBe(true, 'month is earlier')
expect(m.isSameOrAfter(dayjs(new Date(2011, 3, 3, 3, 4, 5, 10)), 'millisecond')).toBe(false, 'day is later')
expect(m.isSameOrAfter(dayjs(new Date(2011, 3, 1, 1, 4, 5, 10)), 'millisecond')).toBe(true, 'day is earlier')
expect(m.isSameOrAfter(dayjs(new Date(2011, 3, 2, 4, 4, 5, 10)), 'millisecond')).toBe(false, 'hour is later')
expect(m.isSameOrAfter(dayjs(new Date(2011, 3, 1, 4, 1, 5, 10)), 'millisecond')).toBe(true, 'hour is earlier')
expect(m.isSameOrAfter(dayjs(new Date(2011, 3, 2, 3, 5, 5, 10)), 'millisecond')).toBe(false, 'minute is later')
expect(m.isSameOrAfter(dayjs(new Date(2011, 3, 2, 3, 3, 5, 10)), 'millisecond')).toBe(true, 'minute is earlier')
expect(m.isSameOrAfter(dayjs(new Date(2011, 3, 2, 3, 4, 6, 10)), 'millisecond')).toBe(false, 'second is later')
expect(m.isSameOrAfter(dayjs(new Date(2011, 3, 2, 3, 4, 4, 5)), 'millisecond')).toBe(true, 'second is earlier')
expect(m.isSameOrAfter(dayjs(new Date(2011, 3, 2, 3, 4, 6, 11)), 'millisecond')).toBe(false, 'millisecond is later')
expect(m.isSameOrAfter(dayjs(new Date(2011, 3, 2, 3, 4, 4, 9)), 'millisecond')).toBe(true, 'millisecond is earlier')
expect(m.isSameOrAfter(m, 'millisecond')).toBe(true, 'same moments are in the same millisecond')
expect(+m).toEqual(+mCopy, 'isSameOrAfter millisecond should not change moment')
})
test('is same or after with invalid moments', () => {
const m = dayjs()
const invalid = dayjs(null)
expect(invalid.isSameOrAfter(invalid)).toBe(false, 'invalid moments are not considered equal')
expect(m.isSameOrAfter(invalid)).toBe(false, 'valid moment is not after invalid moment')
expect(invalid.isSameOrAfter(m)).toBe(false, 'invalid moment is not after valid moment')
expect(m.isSameOrAfter(invalid, 'year')).toBe(false, 'invalid moment year')
expect(m.isSameOrAfter(invalid, 'month')).toBe(false, 'invalid moment month')
expect(m.isSameOrAfter(invalid, 'day')).toBe(false, 'invalid moment day')
expect(m.isSameOrAfter(invalid, 'hour')).toBe(false, 'invalid moment hour')
expect(m.isSameOrAfter(invalid, 'minute')).toBe(false, 'invalid moment minute')
expect(m.isSameOrAfter(invalid, 'second')).toBe(false, 'invalid moment second')
expect(m.isSameOrAfter(invalid, 'milliseconds')).toBe(false, 'invalid moment milliseconds')
})
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/test/plugin/buddhistEra.test.js | test/plugin/buddhistEra.test.js | import MockDate from 'mockdate'
import moment from 'moment'
import dayjs from '../../src'
import buddhistEra from '../../src/plugin/buddhistEra'
dayjs.extend(buddhistEra)
beforeEach(() => {
MockDate.set(new Date())
})
afterEach(() => {
MockDate.reset()
})
it('Format empty string', () => {
expect(dayjs().format()).toBe(moment().format())
})
it('Format Buddhist Era 2 digit', () => {
expect(dayjs().format('BB')).toBe(`${(moment().year() + 543) % 100}`)
})
it('Format Buddhist Era 4 digit', () => {
expect(dayjs().format('BBBB')).toBe(`${moment().year() + 543}`)
})
it('Format Buddhist Era 4 digit with other format', () => {
const format = 'D MMM BBBB'
const today = moment()
const momentDate = today.format(format).replace('BBBB', today.year() + 543)
expect(dayjs().format(format)).toBe(momentDate)
})
it('Skips format strings inside brackets', () => {
expect(dayjs().format('[BBBB]')).toBe('BBBB')
expect(dayjs().format('[BB]')).toBe('BB')
})
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/test/plugin/preParsePostFormat.test.js | test/plugin/preParsePostFormat.test.js | import MockDate from 'mockdate'
import dayjs from '../../src'
import preParsePostFormat from '../../src/plugin/preParsePostFormat'
import localeData from '../../src/plugin/localeData'
import duration from '../../src/plugin/duration'
import calendar from '../../src/plugin/calendar'
import objectSupport from '../../src/plugin/objectSupport'
import customParseFormat from '../../src/plugin/customParseFormat'
import relativeTime from '../../src/plugin/relativeTime'
import utc from '../../src/plugin/utc'
import arraySupport from '../../src/plugin/arraySupport'
import en from '../../src/locale/en'
dayjs.extend(utc)
dayjs.extend(localeData)
dayjs.extend(customParseFormat)
dayjs.extend(arraySupport)
dayjs.extend(objectSupport)
dayjs.extend(calendar)
dayjs.extend(duration)
dayjs.extend(relativeTime)
dayjs.extend(preParsePostFormat)
const symbolMap = {
1: '!',
2: '@',
3: '#',
4: '$',
5: '%',
6: '^',
7: '&',
8: '*',
9: '(',
0: ')'
}
const numberMap = {
'!': '1',
'@': '2',
'#': '3',
$: '4',
'%': '5',
'^': '6',
'&': '7',
'*': '8',
'(': '9',
')': '0'
}
const localeCustomizations = {
...en,
preparse(string) {
if (typeof string !== 'string') {
throw new Error(`preparse - Expected string, got ${typeof string}`)
}
try {
return string.replace(/[!@#$%^&*()]/g, match => numberMap[match])
} catch (error) {
throw new Error(`Unexpected error during preparse of '${string}' - ${error}`)
}
},
postformat(string) {
if (typeof string !== 'string') {
throw new Error(`postformat - Expected string, got ${typeof string}`)
}
try {
return string.replace(/\d/g, match => symbolMap[match])
} catch (error) {
throw new Error(`Unexpected error during postFormat of '${string}' - ${error}`)
}
}
}
beforeEach(() => {
MockDate.set(new Date())
dayjs.locale('symbol', localeCustomizations)
})
afterEach(() => {
MockDate.reset()
dayjs.locale('symbol', null)
})
describe('preparse and postformat', () => {
describe('transform', () => {
const TEST_DATE = '@)!@-)*-@&'
const TEST_NUM = 1346025600
it('preparse string + format', () =>
expect(dayjs.utc(TEST_DATE, 'YYYY-MM-DD').unix()).toBe(TEST_NUM))
it('preparse ISO8601 string', () =>
expect(dayjs.utc(TEST_DATE).unix()).toBe(TEST_NUM))
it('postformat', () =>
expect(dayjs
.unix(TEST_NUM)
.utc()
.format('YYYY-MM-DD'))
.toBe(TEST_DATE))
})
describe('transform from', () => {
dayjs.locale('symbol', localeCustomizations)
const start = dayjs([2007, 1, 28])
const t1 = dayjs([2007, 1, 28]).add({ s: 90 })
it('postformat should work on dayjs.fn.from', () =>
expect(start.from(t1, true)).toBe('@ minutes'))
const t2 = dayjs().add(6, 'd')
it('postformat should work on dayjs.fn.fromNow', () =>
expect(t2.fromNow(true)).toBe('^ days'))
it('postformat should work on dayjs.duration.fn.humanize', () =>
expect(dayjs.duration(10, 'h').humanize()).toBe('!) hours'))
})
})
describe('calendar day', () => {
const a = dayjs()
.hour(12)
.minute(0)
.second(0)
it('today at the same time', () =>
expect(dayjs(a).calendar()).toBe('Today at !@:)) PM'))
it('Now plus 25 min', () =>
expect(dayjs(a)
.add({ m: 25 })
.calendar())
.toBe('Today at !@:@% PM'))
it('Now plus 1 hour', () =>
expect(dayjs(a)
.add({ h: 1 })
.calendar())
.toBe('Today at !:)) PM'))
it('tomorrow at the same time', () =>
expect(dayjs(a)
.add({ d: 1 })
.calendar())
.toBe('Tomorrow at !@:)) PM'))
it('Now minus 1 hour', () =>
expect(dayjs(a)
.subtract({ h: 1 })
.calendar())
.toBe('Today at !!:)) AM'))
it('yesterday at the same time', () =>
expect(dayjs(a)
.subtract({ d: 1 })
.calendar())
.toBe('Yesterday at !@:)) PM'))
})
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/test/plugin/isTomorrow.test.js | test/plugin/isTomorrow.test.js | import MockDate from 'mockdate'
import dayjs from '../../src'
import isTomorrow from '../../src/plugin/isTomorrow'
dayjs.extend(isTomorrow)
beforeEach(() => {
MockDate.set(new Date())
})
afterEach(() => {
MockDate.reset()
})
it('is tomorrow', () => {
expect(dayjs().add(1, 'day').isTomorrow()).toBeTruthy()
expect(dayjs('2017-01-01').isTomorrow('2019-01-01', '2017-01-01')).toBeFalsy()
})
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/test/plugin/objectSupport.test.js | test/plugin/objectSupport.test.js | import MockDate from 'mockdate'
import moment from 'moment'
import dayjs from '../../src'
import objectSupport from '../../src/plugin/objectSupport'
import quarterOfYear from '../../src/plugin/quarterOfYear'
import utc from '../../src/plugin/utc'
import utils from '../../src/utils'
dayjs.extend(utc)
dayjs.extend(quarterOfYear)
dayjs.extend(objectSupport)
beforeEach(() => {
MockDate.set(new Date())
})
afterEach(() => {
MockDate.reset()
})
const now = new Date()
const currentYear = now.getFullYear()
const currentMonth = utils.s(now.getMonth() + 1, 2, '0')
const currentDate = utils.s(now.getDate(), 2, '0')
const currentUTCYear = now.getUTCFullYear()
const currentUTCMonth = utils.s(now.getUTCMonth() + 1, 2, '0')
const currentUTCDate = utils.s(now.getUTCDate(), 2, '0')
const fmt = 'YYYY-MM-DD HH:mm:ss.SSS'
const tests = [
[{ year: 2010 }, '2010-01-01 00:00:00.000'],
[{ year: 2010, month: 1 }, '2010-02-01 00:00:00.000'],
[{ year: 2010, month: 1, day: 12 }, '2010-02-12 00:00:00.000'],
[{ year: 2010, month: 1, date: 12 }, '2010-02-12 00:00:00.000'],
[
{
hour: 15, minute: 25, second: 50, millisecond: 125
},
`${currentYear}-${currentMonth}-${currentDate} 15:25:50.125`,
`${currentUTCYear}-${currentUTCMonth}-${currentUTCDate} 15:25:50.125`],
[
{
year: 2010, month: 1, day: 12, hours: 1
},
'2010-02-12 01:00:00.000'
],
[
{
year: 2010, month: 1, date: 12, hours: 1
},
'2010-02-12 01:00:00.000'
],
[
{
year: 2010, month: 1, day: 12, hours: 1, minutes: 1
},
'2010-02-12 01:01:00.000'
],
[
{
year: 2010, month: 1, date: 12, hours: 1, minutes: 1
},
'2010-02-12 01:01:00.000'
],
[
{
year: 2010,
month: 1,
day: 12,
hours: 1,
minutes: 1,
seconds: 1
},
'2010-02-12 01:01:01.000'
],
[
{
year: 2010,
month: 1,
day: 12,
hours: 1,
minutes: 1,
seconds: 1,
milliseconds: 1
},
'2010-02-12 01:01:01.001'
],
[
{
years: 2010,
months: 1,
days: 14,
hours: 15,
minutes: 25,
seconds: 50,
milliseconds: 125
},
'2010-02-14 15:25:50.125'
],
[
{
year: 2010,
month: 1,
day: 14,
hour: 15,
minute: 25,
second: 50,
millisecond: 125
},
'2010-02-14 15:25:50.125'
],
[
{
y: 2010, M: 1, d: 14, h: 15, m: 25, s: 50, ms: 125
},
'2010-02-14 15:25:50.125'
]
]
describe('parse empty object', () => {
it('local', () => {
expect(dayjs({}).format())
.toBe(moment({}).format())
})
it('utc', () => {
expect(dayjs.utc({}).format())
.toBe(moment.utc({}).format())
})
})
it('Constructor from Object', () => {
for (let i = 0; i < tests.length; i += 1) {
expect(dayjs(tests[i][0]).format(fmt)).toBe(tests[i][1])
expect(moment(tests[i][0]).format(fmt)).toBe(tests[i][1])
}
})
it('Constructor from Object UTC', () => {
for (let i = 0; i < tests.length; i += 1) {
const result = tests[i][2] || tests[i][1]
expect(dayjs.utc(tests[i][0]).format(fmt)).toBe(result)
expect(moment.utc(tests[i][0]).format(fmt)).toBe(result)
}
})
it('Constructor from null should return Invalid Date', () => {
expect(dayjs(null).isValid()).toBe(false)
expect(moment(null).isValid()).toBe(false)
})
it('Set from Object', () => {
for (let i = 0; i < tests.length; i += 1) {
expect(dayjs(now).set(tests[i][0]).format(fmt)).toBe(moment(now).set(tests[i][0]).format(fmt))
}
})
it('add short reverse args', () => {
const a = dayjs({
year: 2011,
month: 9,
date: 12,
hour: 6,
minute: 7,
second: 8,
millisecond: 500
})
expect(a.add({ ms: 50 }).millisecond()).toBe(550)
expect(a.add({ s: 1 }).second()).toBe(9)
expect(a.add({ m: 1 }).minute()).toBe(8)
expect(a.add({ h: 1 }).hour()).toBe(7)
expect(a.add({ d: 1 }).date()).toBe(13)
expect(a.add({ w: 1 }).date()).toBe(19)
expect(a.add({ M: 1 }).month()).toBe(10)
expect(a.add({ y: 1 }).year()).toBe(2012)
expect(a.add({ Q: 1 }).month()).toBe(0)
const aM = moment({
year: 2011,
month: 9,
date: 12,
hour: 6,
minute: 7,
second: 8,
millisecond: 500
})
expect(aM.clone().add({ ms: 50 }).millisecond()).toBe(550)
expect(aM.clone().add({ s: 1 }).second()).toBe(9)
expect(aM.clone().add({ m: 1 }).minute()).toBe(8)
expect(aM.clone().add({ h: 1 }).hour()).toBe(7)
expect(aM.clone().add({ d: 1 }).date()).toBe(13)
expect(aM.clone().add({ w: 1 }).date()).toBe(19)
expect(aM.clone().add({ M: 1 }).month()).toBe(10)
expect(aM.clone().add({ y: 1 }).year()).toBe(2012)
expect(aM.clone().add({ Q: 1 }).month()).toBe(0)
const b = dayjs([2010, 1, 31]).add({ M: 1 })
const c = dayjs([2010, 2, 28]).subtract({ M: 1 })
const d = dayjs([2010, 2, 28]).subtract({ Q: 1 })
expect(b.month()).toBe(1)
expect(b.date()).toBe(28)
expect(c.month()).toBe(0)
expect(c.date()).toBe(28)
expect(d.month()).toBe(10)
expect(d.date()).toBe(28)
expect(d.year()).toBe(2009)
})
it('add long reverse args', () => {
const a = dayjs({
year: 2011,
month: 9,
date: 12,
hour: 6,
minute: 7,
second: 8,
millisecond: 500
})
expect(a.add({ milliseconds: 50 }).millisecond()).toBe(550)
expect(a.add({ seconds: 1 }).second()).toBe(9)
expect(a.add({ minutes: 1 }).minute()).toBe(8)
expect(a.add({ hours: 1 }).hour()).toBe(7)
expect(a.add({ days: 1 }).date()).toBe(13)
expect(a.add({ weeks: 1 }).date()).toBe(19)
expect(a.add({ months: 1 }).month()).toBe(10)
expect(a.add({ years: 1 }).year()).toBe(2012)
expect(a.add({ quarters: 1 }).month()).toBe(0)
})
it('add long singular reverse args', () => {
const a = dayjs({
year: 2011,
month: 9,
date: 12,
hour: 6,
minute: 7,
second: 8,
millisecond: 500
})
expect(a.add({ millisecond: 50 }).millisecond()).toBe(550)
expect(a.add({ second: 1 }).second()).toBe(9)
expect(a.add({ minute: 1 }).minute()).toBe(8)
expect(a.add({ hour: 1 }).hour()).toBe(7)
expect(a.add({ day: 1 }).date()).toBe(13)
expect(a.add({ week: 1 }).date()).toBe(19)
expect(a.add({ month: 1 }).month()).toBe(10)
expect(a.add({ year: 1 }).year()).toBe(2012)
expect(a.add({ quarter: 1 }).month()).toBe(0)
})
it('add string long', () => {
const a = dayjs({
year: 2011,
month: 9,
date: 12,
hour: 6,
minute: 7,
second: 8,
millisecond: 500
})
expect(a.add(50, 'millisecond').millisecond()).toBe(550)
expect(a.add(1, 'second').second()).toBe(9)
expect(a.add(1, 'minute').minute()).toBe(8)
expect(a.add(1, 'hour').hour()).toBe(7)
expect(a.add(1, 'day').date()).toBe(13)
expect(a.add(1, 'week').date()).toBe(19)
expect(a.add(1, 'month').month()).toBe(10)
expect(a.add(1, 'year').year()).toBe(2012)
expect(a.add(1, 'quarter').month()).toBe(0)
})
it('add string long singular', () => {
const a = dayjs({
year: 2011,
month: 9,
date: 12,
hour: 6,
minute: 7,
second: 8,
millisecond: 500
})
expect(a.add(50, 'milliseconds').millisecond()).toBe(550)
expect(a.add(1, 'seconds').second()).toBe(9)
expect(a.add(1, 'minutes').minute()).toBe(8)
expect(a.add(1, 'hours').hour()).toBe(7)
expect(a.add(1, 'days').date()).toBe(13)
expect(a.add(1, 'weeks').date()).toBe(19)
expect(a.add(1, 'months').month()).toBe(10)
expect(a.add(1, 'years').year()).toBe(2012)
expect(a.add(1, 'quarters').month()).toBe(0)
})
it('add string short', () => {
const a = dayjs({
year: 2011,
month: 9,
date: 12,
hour: 6,
minute: 7,
second: 8,
millisecond: 500
})
expect(a.add(50, 'ms').millisecond()).toBe(550)
expect(a.add(1, 's').second()).toBe(9)
expect(a.add(1, 'm').minute()).toBe(8)
expect(a.add(1, 'h').hour()).toBe(7)
expect(a.add(1, 'd').date()).toBe(13)
expect(a.add(1, 'w').date()).toBe(19)
expect(a.add(1, 'M').month()).toBe(10)
expect(a.add(1, 'y').year()).toBe(2012)
expect(a.add(1, 'Q').month()).toBe(0)
})
it('add strings string short', () => {
const a = dayjs({
year: 2011,
month: 9,
date: 12,
hour: 6,
minute: 7,
second: 8,
millisecond: 500
})
expect(a.add('50', 'ms').millisecond()).toBe(550)
expect(a.add('1', 's').second()).toBe(9)
expect(a.add('1', 'm').minute()).toBe(8)
expect(a.add('1', 'h').hour()).toBe(7)
expect(a.add('1', 'd').date()).toBe(13)
expect(a.add('1', 'w').date()).toBe(19)
expect(a.add('1', 'M').month()).toBe(10)
expect(a.add('1', 'y').year()).toBe(2012)
expect(a.add('1', 'Q').month()).toBe(0)
})
it('add no string with milliseconds default', () => {
const a = dayjs({
year: 2011,
month: 9,
date: 12,
hour: 6,
minute: 7,
second: 8,
millisecond: 500
})
expect(a.add(50).millisecond()).toBe(550)
})
it('subtract strings string short', () => {
const a = dayjs({
year: 2011,
month: 9,
date: 12,
hour: 6,
minute: 7,
second: 8,
millisecond: 500
})
expect(a.subtract('50', 'ms').millisecond()).toBe(450)
expect(a.subtract('1', 's').second()).toBe(7)
expect(a.subtract('1', 'm').minute()).toBe(6)
expect(a.subtract('1', 'h').hour()).toBe(5)
expect(a.subtract('1', 'd').date()).toBe(11)
expect(a.subtract('1', 'w').date()).toBe(5)
expect(a.subtract('1', 'M').month()).toBe(8)
expect(a.subtract('1', 'y').year()).toBe(2010)
expect(a.subtract('1', 'Q').month()).toBe(6)
})
it('add decimal values of days and months', () => {
expect(dayjs([2016, 4, 3]).add(1.6, 'days').date()).toBe(5)
expect(dayjs([2016, 4, 3]).add(-1.6, 'days').date()).toBe(1)
expect(dayjs([2016, 4, 1]).add(-1.6, 'days').date()).toBe(30)
expect(dayjs([2016, 4, 3]).add(1.6, 'months').month()).toBe(4)
expect(dayjs([2016, 4, 3]).add(-1.6, 'months').month()).toBe(1)
expect(dayjs([2016, 1, 3]).add(-1.6, 'months').month()).toBe(11)
expect(dayjs([2016, 4, 3]).subtract(1.6, 'days').date()).toBe(1)
expect(dayjs([2016, 4, 2]).subtract(1.6, 'days').date()).toBe(31)
expect(dayjs([2016, 2, 1]).subtract(1.1, 'days').date()).toBe(31)
expect(dayjs([2016, 4, 3]).subtract(-1.6, 'days').date()).toBe(5)
expect(dayjs([2016, 4, 30]).subtract(-1.6, 'days').date()).toBe(2)
expect(dayjs([2016, 4, 3]).subtract(1.6, 'months').month()).toBe(1)
expect(dayjs([2016, 4, 3]).subtract(-1.6, 'months').month()).toBe(4)
expect(dayjs([2016, 12, 31]).subtract(-1.6, 'months').month()).toBe(0)
expect(dayjs([2016, 1, 1]).add(1.6, 'years').format('YYYY-MM-DD')).toBe('2017-01-01')
expect(dayjs([2016, 7, 1]).add(1.6, 'years').format('YYYY-MM-DD')).toBe('2017-07-01')
expect(dayjs([2016, 1, 1]).add(1.1, 'quarters').format('YYYY-MM-DD')).toBe('2016-04-01')
})
it('returns valid date on undefined', () => {
expect(dayjs().isValid()).toBe(true)
})
it('returns invalid date on null', () => {
expect(dayjs(null).isValid()).toBe(false)
})
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/test/plugin/minMax.test.js | test/plugin/minMax.test.js | import MockDate from 'mockdate'
import dayjs from '../../src'
import minMax from '../../src/plugin/minMax'
dayjs.extend(minMax)
beforeEach(() => {
MockDate.set(new Date())
})
afterEach(() => {
MockDate.reset()
})
const arg1 = dayjs('2019-01-01')
const arg2 = dayjs('2018-01-01')
const arg3 = dayjs('2017-01-01')
const arg4 = dayjs('Invalid Date')
it('Return current time if no argument', () => {
expect(dayjs.max())
.toBe(null)
expect(dayjs.min())
.toBe(null)
expect(dayjs.max(null))
.toBe(null)
expect(dayjs.min(null))
.toBe(null)
})
it('Return current time if passing empty array', () => {
expect(dayjs.max([]))
.toBe(null)
expect(dayjs.min([]))
.toBe(null)
})
it('Compare between arguments', () => {
expect(dayjs.max(arg1, arg2, arg3).format())
.toBe(arg1.format())
expect(dayjs.min(arg1, arg2, arg3).format())
.toBe(arg3.format())
})
it('Compare in array', () => {
expect(dayjs.max([arg1, arg2, arg3]).format())
.toBe(arg1.format())
expect(dayjs.min([arg1, arg2, arg3]).format())
.toBe(arg3.format())
})
it('If Invalid Date return Invalid Date', () => {
expect(dayjs.max(arg1, arg2, arg3, arg4).format())
.toBe(arg4.format())
expect(dayjs.min([arg1, arg2, arg3, arg4]).format())
.toBe(arg4.format())
})
it('Ignore if exists an "null" argument', () => {
expect(dayjs.max(null, null, arg1, arg2, null, arg3).format())
.toBe(arg1.format())
expect(dayjs.min([null, null, arg1, arg2, null, arg3]).format())
.toBe(arg3.format())
})
it('Return the only date if just provided one argument', () => {
expect(dayjs.max(arg1).format())
.toBe(arg1.format())
expect(dayjs.min([arg1]).format())
.toBe(arg1.format())
})
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/test/plugin/localizedFormat.test.js | test/plugin/localizedFormat.test.js | import MockDate from 'mockdate'
import moment from 'moment'
import dayjs from '../../src'
import es from '../../src/locale/es'
import znCn from '../../src/locale/zh-cn'
import localizedFormat from '../../src/plugin/localizedFormat'
dayjs.extend(localizedFormat)
beforeEach(() => {
MockDate.set(new Date())
})
afterEach(() => {
MockDate.reset()
})
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('Should not interpolate characters inside square brackets', () => {
const date = new Date(0)
const actualDate = dayjs(date)
const expectedDate = moment(date)
expect(actualDate.format('[l]')).toBe('l')
expect(actualDate.format('YYYY [l] YYYY')).toBe('1970 l 1970')
expect(actualDate.format('l [l] l')).toBe('1/1/1970 l 1/1/1970')
expect(actualDate.format('[L LL LLL LLLL]')).toBe(expectedDate.format('[L LL LLL LLLL]'))
const localeFormats = {
L: '[MMMM MM DD dddd]'
}
const mockedDayJsLocale = {
...es,
name: 'fake-locale',
formats: {
...localeFormats
}
}
const fakeDate = dayjs(date, { locale: mockedDayJsLocale })
expect(fakeDate.locale('fake-locale').format('l')).toEqual('MMMM MM DD dddd')
})
it('Recognizes localized 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)
const mockLocale = {
name: 'mock',
weekdays: Array(7).fill(' '),
months: Array(12).fill(' ')
}
expect(actualDate.locale(mockLocale).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'))
})
it('Uses the localized lowercase formats if defined', () => {
const date = new Date()
const znDate = dayjs(date, { locale: znCn });
['l', 'll', 'lll', 'llll'].forEach(option => expect(znDate.format(option)).toBe(znDate.format(znCn.formats[option])))
})
it('Uses fallback to xx if xx-yy not available', () => {
expect(dayjs('2019-02-01').locale('en-yy').format('MMMM'))
.toBe('February')
})
it('Uses xx-yy if xx-YY is provided', () => {
expect(dayjs('2019-02-01').locale('es-US').format('MMMM'))
.toBe('febrero')
})
it('Uses the localized uppercase formats as a base for lowercase formats, if not defined', () => {
const date = new Date()
const spanishDate = dayjs(date, { locale: es });
['l', 'll', 'lll', 'llll'].forEach((option) => {
const upperCaseFormat = es.formats[option.toUpperCase()]
const adaptedFormat = upperCaseFormat.replace(/(\[[^\]]+])|(MMMM|MM|DD|dddd)/g, (_, a, b) => a || b.slice(1))
expect(spanishDate.format(option)).toBe(spanishDate.format(adaptedFormat))
})
})
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/test/plugin/isYesterday.test.js | test/plugin/isYesterday.test.js | import MockDate from 'mockdate'
import dayjs from '../../src'
import isYesterday from '../../src/plugin/isYesterday'
dayjs.extend(isYesterday)
beforeEach(() => {
MockDate.set(new Date())
})
afterEach(() => {
MockDate.reset()
})
it('is yesterday', () => {
expect(dayjs().subtract(1, 'day').isYesterday()).toBeTruthy()
expect(dayjs('2017-01-01').isYesterday()).toBeFalsy()
})
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/test/plugin/isBetween.test.js | test/plugin/isBetween.test.js | import MockDate from 'mockdate'
import dayjs from '../../src/index'
import isBetween from '../../src/plugin/isBetween/index'
dayjs.extend(isBetween)
beforeEach(() => {
MockDate.set(new Date())
})
afterEach(() => {
MockDate.reset()
})
test('bounds can be swapped', () => {
expect(dayjs('2018-01-01').isBetween(dayjs('2017-12-31'), dayjs('2018-01-02'))).toBeTruthy()
expect(dayjs('2018-01-01').isBetween(dayjs('2018-01-02'), dayjs('2017-12-31'))).toBeTruthy()
})
test('bounds can be swapped with inclusivity', () => {
expect(dayjs('2018-01-01').isBetween(dayjs('2017-12-31'), dayjs('2018-01-01'), null, '[]')).toBeTruthy()
expect(dayjs('2018-01-01').isBetween(dayjs('2018-01-01'), dayjs('2017-12-31'), null, '[]')).toBeTruthy()
})
test('is between without units', () => {
const m = dayjs(new Date(2011, 3, 2, 3, 4, 5, 10))
const mCopy = dayjs(m)
expect(m.isBetween(
dayjs(new Date(2009, 3, 2, 3, 4, 5, 10)),
dayjs(new Date(2011, 3, 2, 3, 4, 5, 10))
)).toBe(false, 'year is later')
expect(m.isBetween(
dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)),
dayjs(new Date(2013, 3, 2, 3, 4, 5, 10))
)).toBe(false, 'year is earlier')
expect(m.isBetween(
dayjs(new Date(2010, 3, 2, 3, 4, 5, 10)),
dayjs(new Date(2012, 3, 2, 3, 4, 5, 10))
)).toBe(true, 'year is between')
expect(m.isBetween(
dayjs(new Date(2011, 1, 2, 3, 4, 5, 10)),
dayjs(new Date(2011, 3, 2, 3, 4, 5, 10))
)).toBe(false, 'month is later')
expect(m.isBetween(
dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)),
dayjs(new Date(2011, 5, 2, 3, 4, 5, 10))
)).toBe(false, 'month is earlier')
expect(m.isBetween(
dayjs(new Date(2011, 2, 2, 3, 4, 5, 10)),
dayjs(new Date(2011, 4, 2, 3, 4, 5, 10))
)).toBe(true, 'month is between')
expect(m.isBetween(
dayjs(new Date(2011, 3, 1, 3, 4, 5, 10)),
dayjs(new Date(2011, 3, 2, 3, 4, 5, 10))
)).toBe(false, 'day is later')
expect(m.isBetween(
dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)),
dayjs(new Date(2011, 3, 4, 3, 4, 5, 10))
)).toBe(false, 'day is earlier')
expect(m.isBetween(
dayjs(new Date(2011, 3, 1, 3, 4, 5, 10)),
dayjs(new Date(2011, 3, 3, 3, 4, 5, 10))
)).toBe(true, 'day is between')
expect(m.isBetween(
dayjs(new Date(2011, 3, 2, 1, 4, 5, 10)),
dayjs(new Date(2011, 3, 2, 3, 4, 5, 10))
)).toBe(false, 'hour is later')
expect(m.isBetween(
dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)),
dayjs(new Date(2011, 3, 2, 5, 4, 5, 10))
)).toBe(false, 'hour is earlier')
expect(m.isBetween(
dayjs(new Date(2011, 3, 2, 2, 4, 5, 10)),
dayjs(new Date(2011, 3, 2, 4, 4, 5, 10))
)).toBe(true, 'hour is between')
expect(m.isBetween(
dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)),
dayjs(new Date(2011, 3, 2, 3, 6, 5, 10))
)).toBe(false, 'minute is later')
expect(m.isBetween(
dayjs(new Date(2011, 3, 2, 3, 2, 5, 10)),
dayjs(new Date(2011, 3, 2, 3, 4, 5, 10))
)).toBe(false, 'minute is earlier')
expect(m.isBetween(
dayjs(new Date(2011, 3, 2, 3, 3, 5, 10)),
dayjs(new Date(2011, 3, 2, 3, 5, 5, 10))
)).toBe(true, 'minute is between')
expect(m.isBetween(
dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)),
dayjs(new Date(2011, 3, 2, 3, 4, 7, 10))
)).toBe(false, 'second is later')
expect(m.isBetween(
dayjs(new Date(2011, 3, 2, 3, 4, 3, 10)),
dayjs(new Date(2011, 3, 2, 3, 4, 5, 10))
)).toBe(false, 'second is earlier')
expect(m.isBetween(
dayjs(new Date(2011, 3, 2, 3, 4, 4, 10)),
dayjs(new Date(2011, 3, 2, 3, 4, 6, 10))
)).toBe(true, 'second is between')
expect(m.isBetween(
dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)),
dayjs(new Date(2011, 3, 2, 3, 4, 5, 12))
)).toBe(false, 'millisecond is later')
expect(m.isBetween(
dayjs(new Date(2011, 3, 2, 3, 4, 5, 8)),
dayjs(new Date(2011, 3, 2, 3, 4, 5, 10))
)).toBe(false, 'millisecond is earlier')
expect(m.isBetween(
dayjs(new Date(2011, 3, 2, 3, 4, 5, 9)),
dayjs(new Date(2011, 3, 2, 3, 4, 5, 11))
)).toBe(true, 'millisecond is between')
expect(m.isBetween(m, m)).toBe(false, 'moments are not between themselves')
expect(+m).toEqual(+mCopy, 'isBetween second should not change moment')
})
test('is between year', () => {
const m = dayjs(new Date(2011, 1, 2, 3, 4, 5, 6))
const mCopy = dayjs(m)
expect(m.isBetween(
dayjs(new Date(2011, 5, 6, 7, 8, 9, 10)),
dayjs(new Date(2011, 5, 6, 7, 8, 9, 10)),
'year'
)).toBe(false, 'year match')
expect(m.isBetween(
dayjs(new Date(2010, 5, 6, 7, 8, 9, 10)),
dayjs(new Date(2012, 5, 6, 7, 8, 9, 10)),
'years'
)).toBe(true, 'plural should work')
expect(m.isBetween(
dayjs(new Date(2010, 5, 6, 7, 8, 9, 10)),
dayjs(new Date(2012, 5, 6, 7, 8, 9, 10)),
'year'
)).toBe(true, 'year is between')
expect(m.isBetween(
dayjs(new Date(2011, 5, 6, 7, 8, 9, 10)),
dayjs(new Date(2013, 5, 6, 7, 8, 9, 10)),
'year'
)).toBe(false, 'year is earlier')
expect(m.isBetween(
dayjs(new Date(2010, 5, 6, 7, 8, 9, 10)),
dayjs(new Date(2011, 5, 6, 7, 8, 9, 10)),
'year'
)).toBe(false, 'year is later')
expect(m.isBetween(m, 'year')).toBe(false, 'same moments are not between the same year')
expect(+m).toEqual(+mCopy, 'isBetween year should not change moment')
})
test('is between month', () => {
const m = dayjs(new Date(2011, 1, 2, 3, 4, 5, 6))
const mCopy = dayjs(m)
expect(m.isBetween(
dayjs(new Date(2011, 1, 6, 7, 8, 9, 10)),
dayjs(new Date(2011, 1, 6, 7, 8, 9, 10)),
'month'
)).toBe(false, 'month match')
expect(m.isBetween(
dayjs(new Date(2011, 0, 6, 7, 8, 9, 10)),
dayjs(new Date(2011, 2, 6, 7, 8, 9, 10)),
'months'
)).toBe(true, 'plural should work')
expect(m.isBetween(
dayjs(new Date(2011, 0, 31, 23, 59, 59, 999)),
dayjs(new Date(2011, 2, 1, 0, 0, 0, 0)),
'month'
)).toBe(true, 'month is between')
expect(m.isBetween(
dayjs(new Date(2011, 1, 6, 7, 8, 9, 10)),
dayjs(new Date(2011, 2, 6, 7, 8, 9, 10)),
'month'
)).toBe(false, 'month is earlier')
expect(m.isBetween(
dayjs(new Date(2011, 11, 6, 7, 8, 9, 10)),
dayjs(new Date(2011, 1, 6, 7, 8, 9, 10)),
'month'
)).toBe(false, 'month is later')
expect(m.isBetween(m, 'month')).toBe(false, 'same moments are not between the same month')
expect(+m).toEqual(+mCopy, 'isBetween month should not change moment')
})
test('is between day', () => {
const m = dayjs(new Date(2011, 1, 2, 3, 4, 5, 6))
const mCopy = dayjs(m)
expect(m.isBetween(
dayjs(new Date(2011, 1, 2, 7, 8, 9, 10)),
dayjs(new Date(2011, 1, 2, 7, 8, 9, 10)),
'day'
)).toBe(false, 'day match')
expect(m.isBetween(
dayjs(new Date(2011, 1, 1, 7, 8, 9, 10)),
dayjs(new Date(2011, 1, 3, 7, 8, 9, 10)),
'days'
)).toBe(true, 'plural should work')
expect(m.isBetween(
dayjs(new Date(2011, 1, 1, 7, 8, 9, 10)),
dayjs(new Date(2011, 1, 3, 7, 8, 9, 10)),
'day'
)).toBe(true, 'day is between')
expect(m.isBetween(
dayjs(new Date(2011, 1, 2, 7, 8, 9, 10)),
dayjs(new Date(2011, 1, 4, 7, 8, 9, 10)),
'day'
)).toBe(false, 'day is earlier')
expect(m.isBetween(
dayjs(new Date(2011, 1, 1, 7, 8, 9, 10)),
dayjs(new Date(2011, 1, 2, 7, 8, 9, 10)),
'day'
)).toBe(false, 'day is later')
expect(m.isBetween(m, 'day')).toBe(false, 'same moments are not between the same day')
expect(+m).toEqual(+mCopy, 'isBetween day should not change moment')
})
test('is between hour', () => {
const m = dayjs(new Date(2011, 1, 2, 3, 4, 5, 6))
const mCopy = dayjs(m)
expect(m.isBetween(
dayjs(new Date(2011, 1, 2, 3, 5, 9, 10)),
dayjs(new Date(2011, 1, 2, 3, 9, 9, 10)), 'hour'
))
.toBe(false, 'hour match')
expect(m.isBetween(
dayjs(new Date(2011, 1, 2, 1, 59, 59, 999)),
dayjs(new Date(2011, 1, 2, 4, 0, 0, 0)), 'hours'
))
.toBe(true, 'plural should work')
expect(m.isBetween(
dayjs(new Date(2011, 1, 2, 2, 59, 59, 999)),
dayjs(new Date(2011, 1, 2, 4, 0, 0, 0)), 'hour'
))
.toBe(true, 'hour is between')
expect(m.isBetween(
dayjs(new Date(2011, 1, 2, 7, 8, 9, 10)),
dayjs(new Date(2011, 1, 2, 7, 8, 9, 10)), 'hour'
))
.toBe(false, 'hour is earlier')
expect(m.isBetween(
dayjs(new Date(2011, 1, 2, 7, 8, 9, 10)),
dayjs(new Date(2011, 1, 2, 7, 8, 9, 10)), 'hour'
))
.toBe(false, 'hour is later')
expect(m.isBetween(m, 'hour'))
.toBe(false, 'same moments are not between the same hour')
expect(+m)
.toEqual(+mCopy, 'isBetween hour should not change moment')
})
test('is between minute', () => {
const m = dayjs(new Date(2011, 1, 2, 3, 4, 5, 6))
const mCopy = dayjs(m)
expect(m.isBetween(
dayjs(new Date(2011, 1, 2, 3, 4, 9, 10)),
dayjs(new Date(2011, 1, 2, 3, 4, 9, 10)), 'minute'
))
.toBe(false, 'minute match')
expect(m.isBetween(
dayjs(new Date(2011, 1, 2, 3, 3, 9, 10)),
dayjs(new Date(2011, 1, 2, 3, 5, 9, 10)), 'minutes'
))
.toBe(true, 'plural should work')
expect(m.isBetween(
dayjs(new Date(2011, 1, 2, 3, 3, 59, 999)),
dayjs(new Date(2011, 1, 2, 3, 5, 0, 0)), 'minute'
))
.toBe(true, 'minute is between')
expect(m.isBetween(
dayjs(new Date(2011, 1, 2, 3, 5, 0, 0)),
dayjs(new Date(2011, 1, 2, 3, 8, 9, 10)), 'minute'
))
.toBe(false, 'minute is earlier')
expect(m.isBetween(
dayjs(new Date(2011, 1, 2, 3, 2, 9, 10)),
dayjs(new Date(2011, 1, 2, 3, 3, 59, 999)), 'minute'
))
.toBe(false, 'minute is later')
expect(m.isBetween(m, 'minute'))
.toBe(false, 'same moments are not between the same minute')
expect(+m)
.toEqual(+mCopy, 'isBetween minute should not change moment')
})
test('is between second', () => {
const m = dayjs(new Date(2011, 1, 2, 3, 4, 5, 6))
const mCopy = dayjs(m)
expect(m.isBetween(
dayjs(new Date(2011, 1, 2, 3, 4, 5, 10)),
dayjs(new Date(2011, 1, 2, 3, 4, 5, 10)), 'second'
))
.toBe(false, 'second match')
expect(m.isBetween(
dayjs(new Date(2011, 1, 2, 3, 4, 4, 10)),
dayjs(new Date(2011, 1, 2, 3, 4, 6, 10)), 'seconds'
))
.toBe(true, 'plural should work')
expect(m.isBetween(
dayjs(new Date(2011, 1, 2, 3, 4, 4, 999)),
dayjs(new Date(2011, 1, 2, 3, 4, 6, 0)), 'second'
))
.toBe(true, 'second is between')
expect(m.isBetween(
dayjs(new Date(2011, 1, 2, 3, 4, 6, 0)),
dayjs(new Date(2011, 1, 2, 3, 4, 7, 10)), 'second'
))
.toBe(false, 'second is earlier')
expect(m.isBetween(
dayjs(new Date(2011, 1, 2, 3, 4, 3, 10)),
dayjs(new Date(2011, 1, 2, 3, 4, 4, 999)), 'second'
))
.toBe(false, 'second is later')
expect(m.isBetween(m, 'second')).toBe(false, 'same moments are not between the same second')
expect(+m).toEqual(+mCopy, 'isBetween second should not change moment')
})
test('is between millisecond', () => {
const m = dayjs(new Date(2011, 1, 2, 3, 4, 5, 6))
const mCopy = dayjs(m)
expect(m.isBetween(
dayjs(new Date(2011, 1, 2, 3, 4, 5, 6)),
dayjs(new Date(2011, 1, 2, 3, 4, 5, 6)),
'millisecond'
)).toBe(false, 'millisecond match')
expect(m.isBetween(
dayjs(new Date(2011, 1, 2, 3, 4, 5, 5)),
dayjs(new Date(2011, 1, 2, 3, 4, 5, 7)),
'milliseconds'
)).toBe(true, 'plural should work')
expect(m.isBetween(
dayjs(new Date(2011, 1, 2, 3, 4, 5, 5)),
dayjs(new Date(2011, 1, 2, 3, 4, 5, 7)),
'millisecond'
)).toBe(true, 'millisecond is between')
expect(m.isBetween(
dayjs(new Date(2011, 1, 2, 3, 4, 5, 7)),
dayjs(new Date(2011, 1, 2, 3, 4, 5, 10)),
'millisecond'
)).toBe(false, 'millisecond is earlier')
expect(m.isBetween(
dayjs(new Date(2011, 1, 2, 3, 4, 5, 4)),
dayjs(new Date(2011, 1, 2, 3, 4, 5, 6)),
'millisecond'
)).toBe(false, 'millisecond is later')
expect(m.isBetween(m, 'millisecond')).toBe(false, 'same moments are not between the same millisecond')
expect(+m).toEqual(+mCopy, 'isBetween millisecond should not change moment')
})
test('is between without units inclusivity', () => {
const m = dayjs(new Date(2011, 3, 2, 3, 4, 5, 10))
const mCopy = dayjs(m)
expect(m.isBetween(
dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)),
dayjs(new Date(2012, 3, 2, 3, 4, 5, 10)),
null,
'()'
)).toBe(false, 'start and end are excluded, start is equal to dayjs')
expect(m.isBetween(
dayjs(new Date(2010, 3, 2, 3, 4, 5, 10)),
dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)),
null,
'()'
)).toBe(false, 'start and end are excluded, end is equal to dayjs')
expect(m.isBetween(
dayjs(new Date(2010, 3, 2, 3, 4, 5, 10)),
dayjs(new Date(2012, 3, 2, 3, 4, 5, 10)),
null,
'()'
)).toBe(true, 'start and end are excluded, is between')
expect(m.isBetween(
dayjs(new Date(2009, 3, 2, 3, 4, 5, 10)),
dayjs(new Date(2010, 3, 2, 3, 4, 5, 10)),
null,
'()'
)).toBe(false, 'start and end are excluded, is not between')
expect(m.isBetween(
dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)),
dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)),
null,
'()'
)).toBe(false, 'start and end are excluded, should fail on same start/end date.')
expect(m.isBetween(
dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)),
dayjs(new Date(2012, 3, 2, 3, 4, 5, 10)),
null,
'(]'
)).toBe(false, 'start is excluded and end is included should fail on same start date')
expect(m.isBetween(
dayjs(new Date(2010, 3, 2, 3, 4, 5, 10)),
dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)),
null,
'(]'
)).toBe(true, 'start is excluded and end is included should succeed on end date')
expect(m.isBetween(
dayjs(new Date(2010, 3, 2, 3, 4, 5, 10)),
dayjs(new Date(2012, 3, 2, 3, 4, 5, 10)),
null,
'(]'
)).toBe(true, 'start is excluded and end is included, is between')
expect(m.isBetween(
dayjs(new Date(2009, 3, 2, 3, 4, 5, 10)),
dayjs(new Date(2010, 3, 2, 3, 4, 5, 10)),
null,
'(]'
)).toBe(false, 'start is excluded and end is included, is not between')
expect(m.isBetween(
dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)),
dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)),
null,
'(]'
)).toBe(false, 'start is excluded and end is included, should fail on same start/end date.')
expect(m.isBetween(
dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)),
dayjs(new Date(2012, 3, 2, 3, 4, 5, 10)),
null,
'[)'
)).toBe(true, 'start is included and end is excluded should succeed on same start date')
expect(m.isBetween(
dayjs(new Date(2010, 3, 2, 3, 4, 5, 10)),
dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)),
null,
'[)'
)).toBe(false, 'start is included and end is excluded should fail on same end date')
expect(m.isBetween(
dayjs(new Date(2010, 3, 2, 3, 4, 5, 10)),
dayjs(new Date(2012, 3, 2, 3, 4, 5, 10)),
null,
'[)'
)).toBe(true, 'start is included and end is excluded, is between')
expect(m.isBetween(
dayjs(new Date(2009, 3, 2, 3, 4, 5, 10)),
dayjs(new Date(2010, 3, 2, 3, 4, 5, 10)),
null,
'[)'
)).toBe(false, 'start is included and end is excluded, is not between')
expect(m.isBetween(
dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)),
dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)),
null,
'[)'
)).toBe(false, 'start is included and end is excluded, should fail on same end and start date')
expect(m.isBetween(
dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)),
dayjs(new Date(2012, 3, 2, 3, 4, 5, 10)),
null,
'[]'
)).toBe(true, 'start and end inclusive should succeed on same start date')
expect(m.isBetween(
dayjs(new Date(2010, 3, 2, 3, 4, 5, 10)),
dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)),
null,
'[]'
)).toBe(true, 'start and end inclusive should succeed on same end date')
expect(m.isBetween(
dayjs(new Date(2010, 3, 2, 3, 4, 5, 10)),
dayjs(new Date(2012, 3, 2, 3, 4, 5, 10)),
null,
'[]'
)).toBe(true, 'start and end inclusive, is between')
expect(m.isBetween(
dayjs(new Date(2009, 3, 2, 3, 4, 5, 10)),
dayjs(new Date(2010, 3, 2, 3, 4, 5, 10)),
null,
'[]'
)).toBe(false, 'start and end inclusive, is not between')
expect(m.isBetween(
dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)),
dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)),
null,
'[]'
)).toBe(true, 'start and end inclusive, should handle same end and start date')
expect(+m).toEqual(+mCopy, 'isBetween millisecond should not change moment')
})
test('is between milliseconds inclusivity', () => {
const m = dayjs(new Date(2011, 3, 2, 3, 4, 5, 10))
const mCopy = dayjs(m)
expect(m.isBetween(
dayjs(new Date(2010, 3, 2, 3, 4, 5, 10)),
dayjs(new Date(2012, 3, 2, 3, 4, 5, 10)),
'milliseconds'
)).toBe(true, 'options, no inclusive')
expect(m.isBetween(
dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)),
dayjs(new Date(2012, 3, 2, 3, 4, 5, 10)),
'milliseconds',
'()'
)).toBe(false, 'start and end are excluded, start is equal to dayjs')
expect(m.isBetween(
dayjs(new Date(2010, 3, 2, 3, 4, 5, 10)),
dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)),
'milliseconds',
'()'
)).toBe(false, 'start and end are excluded, end is equal to dayjs')
expect(m.isBetween(
dayjs(new Date(2010, 3, 2, 3, 4, 5, 10)),
dayjs(new Date(2012, 3, 2, 3, 4, 5, 10)),
'milliseconds',
'()'
)).toBe(true, 'start and end are excluded, is between')
expect(m.isBetween(
dayjs(new Date(2009, 3, 2, 3, 4, 5, 10)),
dayjs(new Date(2010, 3, 2, 3, 4, 5, 10)),
'milliseconds',
'()'
)).toBe(false, 'start and end are excluded, is not between')
expect(m.isBetween(
dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)),
dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)),
'milliseconds',
'()'
)).toBe(false, 'start and end are excluded, should fail on same start/end date.')
expect(m.isBetween(
dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)),
dayjs(new Date(2012, 3, 2, 3, 4, 5, 10)),
'milliseconds',
'(]'
)).toBe(false, 'start is excluded and end is included should fail on same start date')
expect(m.isBetween(
dayjs(new Date(2010, 3, 2, 3, 4, 5, 10)),
dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)),
'milliseconds',
'(]'
)).toBe(true, 'start is excluded and end is included should succeed on end date')
expect(m.isBetween(
dayjs(new Date(2010, 3, 2, 3, 4, 5, 10)),
dayjs(new Date(2012, 3, 2, 3, 4, 5, 10)),
'milliseconds',
'(]'
)).toBe(true, 'start is excluded and end is included, is between')
expect(m.isBetween(
dayjs(new Date(2009, 3, 2, 3, 4, 5, 10)),
dayjs(new Date(2010, 3, 2, 3, 4, 5, 10)),
'milliseconds',
'(]'
)).toBe(false, 'start is excluded and end is included, is not between')
expect(m.isBetween(
dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)),
dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)),
'milliseconds',
'(]'
)).toBe(false, 'start is excluded and end is included, should fail on same start/end date.')
expect(m.isBetween(
dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)),
dayjs(new Date(2012, 3, 2, 3, 4, 5, 10)),
'milliseconds',
'[)'
)).toBe(true, 'start is included and end is excluded should succeed on same start date')
expect(m.isBetween(
dayjs(new Date(2010, 3, 2, 3, 4, 5, 10)),
dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)),
'milliseconds',
'[)'
)).toBe(false, 'start is included and end is excluded should fail on same end date')
expect(m.isBetween(
dayjs(new Date(2010, 3, 2, 3, 4, 5, 10)),
dayjs(new Date(2012, 3, 2, 3, 4, 5, 10)),
'milliseconds',
'[)'
)).toBe(true, 'start is included and end is excluded, is between')
expect(m.isBetween(
dayjs(new Date(2009, 3, 2, 3, 4, 5, 10)),
dayjs(new Date(2010, 3, 2, 3, 4, 5, 10)),
'milliseconds',
'[)'
)).toBe(false, 'start is included and end is excluded, is not between')
expect(m.isBetween(
dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)),
dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)),
'milliseconds',
'[)'
)).toBe(false, 'start is included and end is excluded, should fail on same end and start date')
expect(m.isBetween(
dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)),
dayjs(new Date(2012, 3, 2, 3, 4, 5, 10)),
'milliseconds',
'[]'
)).toBe(true, 'start and end inclusive should succeed on same start date')
expect(m.isBetween(
dayjs(new Date(2010, 3, 2, 3, 4, 5, 10)),
dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)),
'milliseconds',
'[]'
)).toBe(true, 'start and end inclusive should succeed on same end date')
expect(m.isBetween(
dayjs(new Date(2010, 3, 2, 3, 4, 5, 10)),
dayjs(new Date(2012, 3, 2, 3, 4, 5, 10)),
'milliseconds',
'[]'
)).toBe(true, 'start and end inclusive, is between')
expect(m.isBetween(
dayjs(new Date(2009, 3, 2, 3, 4, 5, 10)),
dayjs(new Date(2010, 3, 2, 3, 4, 5, 10)),
'milliseconds',
'[]'
)).toBe(false, 'start and end inclusive, is not between')
expect(m.isBetween(
dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)),
dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)),
'milliseconds',
'[]'
)).toBe(true, 'start and end inclusive, should handle same end and start date')
expect(+m).toEqual(+mCopy, 'isBetween second should not change moment')
})
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/test/plugin/devHelper.test.js | test/plugin/devHelper.test.js | import MockDate from 'mockdate'
import dayjs from '../../src'
import customParseFormat from '../../src/plugin/customParseFormat'
import devHelper from '../../src/plugin/devHelper'
import localeData from '../../src/plugin/localeData'
dayjs.extend(devHelper)
beforeEach(() => {
MockDate.set(new Date())
})
afterEach(() => {
MockDate.reset()
})
global.console.warn = jest.genMockFunction()
it('Warning: passing Year as a Number will be parsed as a Unix timestamp', () => {
const consoleSpy = jest.spyOn(console, 'warn')
dayjs(2020)
expect(consoleSpy).toHaveBeenCalledWith('Guessing you may want to parse the Year 2020, you should pass it as a String 2020, not a Number. Otherwise, 2020 will be treated as a Unix timestamp')
})
it('Warning Passing Unix timestamp as a String not Number', () => {
const consoleSpy = jest.spyOn(console, 'warn')
dayjs('1231231231231')
expect(consoleSpy).toHaveBeenCalledWith('To parse a Unix timestamp like 1231231231231, you should pass it as a Number. https://day.js.org/docs/en/parse/unix-timestamp-milliseconds')
})
it('Warning Enable customParseFormat plugin while passing the second format parameter', () => {
const consoleSpy = jest.spyOn(console, 'warn')
dayjs('2020', 'YYYY')
expect(consoleSpy).toHaveBeenCalledWith('To parse a date-time string like 2020 using the given format, you should enable customParseFormat plugin first. https://day.js.org/docs/en/parse/string-format')
})
it('Warning: Setting locale before loading locale', () => {
const consoleSpy = jest.spyOn(console, 'warn')
dayjs.locale('zh-cn')
expect(consoleSpy).toHaveBeenCalledWith('Guessing you may want to use locale zh-cn, you have to load it before using it. https://day.js.org/docs/en/i18n/loading-into-nodejs')
})
describe('dev-helper: diff() usage warnings', () => {
const diffWarningMsg = 'Invalid usage: diff() requires a valid comparison date as the first argument. https://day.js.org/docs/en/display/difference'
beforeAll(() => {
dayjs.extend(customParseFormat)
dayjs.extend(localeData)
})
beforeEach(() => {
jest.clearAllMocks()
})
it('warns when diff() is called with no comparison date', () => {
const consoleSpy = jest.spyOn(console, 'warn')
dayjs('2025-01-10').diff()
expect(consoleSpy).toHaveBeenCalledWith(diffWarningMsg)
})
it('warns when diff() is called with just the unit', () => {
const consoleSpy = jest.spyOn(console, 'warn')
dayjs('2025-01-10').diff('days')
expect(consoleSpy).toHaveBeenCalledWith(diffWarningMsg)
})
it('warns when diff() is called with an invalid comparison date (unparsable string)', () => {
const consoleSpy = jest.spyOn(console, 'warn')
dayjs('2025-01-10').diff('invalid-date', 'days')
expect(consoleSpy).toHaveBeenCalledWith(diffWarningMsg)
})
it('does NOT warn when diff() is called with a valid string date', () => {
const consoleSpy = jest.spyOn(console, 'warn')
dayjs('2025-01-10').diff('2025-01-09', 'days')
expect(consoleSpy).not.toHaveBeenCalledWith(diffWarningMsg)
})
it('does NOT warn when diff() is called with a valid Day.js instance', () => {
const consoleSpy = jest.spyOn(console, 'warn')
dayjs('2025-01-10').diff(dayjs(), 'days')
expect(consoleSpy).not.toHaveBeenCalledWith(diffWarningMsg)
})
})
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/test/plugin/pluralGetSet.test.js | test/plugin/pluralGetSet.test.js | import MockDate from 'mockdate'
import moment from 'moment'
import dayjs from '../../src'
import pluralGetSet from '../../src/plugin/pluralGetSet'
dayjs.extend(pluralGetSet)
const warnBackup = global.console.warn
beforeEach(() => {
MockDate.set(new Date())
global.console.warn = jest.genMockFunction()
// moment.js .years, .dates, .months will throw warn
})
afterEach(() => {
MockDate.reset()
global.console.warn = warnBackup
})
it('Years', () => {
expect(dayjs().get('years')).toBe(moment().get('years'))
expect(dayjs().years()).toBe(moment().years())
expect(dayjs().years(0).valueOf()).toBe(moment().years(0).valueOf())
expect(dayjs().years(2000).valueOf()).toBe(moment().years(2000).valueOf())
})
it('Months', () => {
expect(dayjs().get('months')).toBe(moment().get('months'))
expect(dayjs().months()).toBe(moment().months())
expect(dayjs().months(0).valueOf()).toBe(moment().months(0).valueOf())
expect(dayjs().months(1).valueOf()).toBe(moment().months(1).valueOf())
})
it('Days of Week', () => {
expect(dayjs().get('days')).toBe(moment().get('days'))
expect(dayjs().days()).toBe(moment().days())
expect(dayjs().days(0).format()).toBe(moment().days(0).format())
expect(dayjs().days(1).format()).toBe(moment().days(1).format())
})
it('Dates', () => {
expect(dayjs().get('dates')).toBe(moment().get('dates'))
expect(dayjs().dates()).toBe(moment().dates())
expect(dayjs().dates(0).valueOf()).toBe(moment().dates(0).valueOf())
expect(dayjs().dates(1).valueOf()).toBe(moment().dates(1).valueOf())
})
it('Hours', () => {
expect(dayjs().get('hours')).toBe(moment().get('hours'))
expect(dayjs().hours()).toBe(moment().hours())
expect(dayjs().hours(0).valueOf()).toBe(moment().hours(0).valueOf())
expect(dayjs().hours(1).valueOf()).toBe(moment().hours(1).valueOf())
})
it('Minutes', () => {
expect(dayjs().get('minutes')).toBe(moment().get('minutes'))
expect(dayjs().minutes()).toBe(moment().minutes())
expect(dayjs().minutes(0).valueOf()).toBe(moment().minutes(0).valueOf())
expect(dayjs().minutes(1).valueOf()).toBe(moment().minutes(1).valueOf())
})
it('Seconds', () => {
expect(dayjs().get('seconds')).toBe(moment().get('seconds'))
expect(dayjs().seconds()).toBe(moment().seconds())
expect(dayjs().seconds(0).valueOf()).toBe(moment().seconds(0).valueOf())
expect(dayjs().seconds(1).valueOf()).toBe(moment().seconds(1).valueOf())
})
it('Milliseconds', () => {
expect(dayjs().get('milliseconds')).toBe(moment().get('milliseconds'))
expect(dayjs().milliseconds()).toBe(moment().milliseconds())
expect(dayjs().milliseconds(0).valueOf()).toBe(moment().milliseconds(0).valueOf())
expect(dayjs().milliseconds(1).valueOf()).toBe(moment().milliseconds(1).valueOf())
})
it('Set Dates', () => {
expect(dayjs().date(30).valueOf()).toBe(moment().dates(30).valueOf())
expect(dayjs().set('dates', 30).valueOf()).toBe(moment().set('dates', 30).valueOf())
})
it('Set Days of Week', () => {
expect(dayjs().days(0).valueOf()).toBe(moment().days(0).valueOf())
expect(dayjs().set('days', 0).valueOf()).toBe(moment().set('days', 0).valueOf())
})
it('Set Months', () => {
expect(dayjs().months(11).valueOf()).toBe(moment().months(11).valueOf())
expect(dayjs().set('months', 11).valueOf()).toBe(moment().set('months', 11).valueOf())
})
it('Set Years', () => {
expect(dayjs().years(2008).valueOf()).toBe(moment().year(2008).valueOf())
expect(dayjs().set('years', 2008).valueOf()).toBe(moment().set('years', 2008).valueOf())
})
it('Set Hours', () => {
expect(dayjs().set('hours', 6).valueOf()).toBe(moment().set('hours', 6).valueOf())
expect(dayjs().hours(6).valueOf()).toBe(moment().hours(6).valueOf())
})
it('Set Minutes', () => {
expect(dayjs().minutes(59).valueOf()).toBe(moment().minutes(59).valueOf())
expect(dayjs().set('minutes', 59).valueOf()).toBe(moment().set('minutes', 59).valueOf())
})
it('Set Seconds', () => {
expect(dayjs().seconds(59).valueOf()).toBe(moment().seconds(59).valueOf())
expect(dayjs().set('second', 59).valueOf()).toBe(moment().set('second', 59).valueOf())
})
it('Set Milliseconds', () => {
expect(dayjs().milliseconds(999).valueOf()).toBe(moment().milliseconds(999).valueOf())
expect(dayjs().set('millisecond', 999).valueOf()).toBe(moment().set('millisecond', 999).valueOf())
})
it('Set Month and Year in last day of month', () => {
// 2011-07-31 -> 2011-02-28
const origin = dayjs('2011-07-31T14:48:00.000Z')
const setMonth = origin.set('month', 1)
expect(setMonth.months()).toBe(1)
expect(origin.dates()).toBe(31)
expect(setMonth.dates()).toBe(28)
// 2000-02-29 -> 2001-02-28
const origin2 = dayjs('2000-02-29T14:48:00.000Z')
const setYear = origin2.set('years', 2001)
expect(setYear.months()).toBe(1)
expect(origin2.dates()).toBe(29)
expect(setYear.dates()).toBe(28)
})
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/test/plugin/isLeapYear.test.js | test/plugin/isLeapYear.test.js | 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)
})
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/test/plugin/declarations.test.js | test/plugin/declarations.test.js | import fs from 'fs'
import path from 'path'
const pluginDir = '../../src/plugin'
const pluginTypeDir = '../../types/plugin'
it('Plugin declarations', () => {
fs.readdirSync(path.join(__dirname, pluginDir))
.forEach((l) => {
expect(fs.existsSync(path.join(__dirname, pluginTypeDir, `${l}.d.ts`)))
.toBe(true)
})
})
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/test/plugin/utc-utcOffset.test.js | test/plugin/utc-utcOffset.test.js | import MockDate from 'mockdate'
import moment from 'moment'
import dayjs from '../../src'
import utc from '../../src/plugin/utc'
dayjs.extend(utc)
beforeEach(() => {
MockDate.set(new Date())
})
afterEach(() => {
MockDate.reset()
})
it('Set utcOffset -> Get utcOffset', () => {
expect(dayjs().utcOffset(540).utcOffset()).toBe(moment().utcOffset(540).utcOffset())
expect(dayjs().utcOffset(540).format()).toBe(moment().utcOffset(540).format())
expect(dayjs().utcOffset(60).format()).toBe(moment().utcOffset(60).format())
expect(dayjs().utcOffset(8).format()).toBe(moment().utcOffset(8).format())
expect(dayjs().utcOffset(-540).utcOffset()).toBe(moment().utcOffset(-540).utcOffset())
expect(dayjs().utcOffset(-540).format()).toBe(moment().utcOffset(-540).format())
expect(dayjs().utcOffset(-60).format()).toBe(moment().utcOffset(-60).format())
expect(dayjs().utcOffset(-8).format()).toBe(moment().utcOffset(-8).format())
})
it('valueOf, toDate, toString, toISOString should be the same as original', () => {
const d = dayjs()
const du = dayjs().utcOffset(9)
const mu = moment().utcOffset(9)
expect(d.valueOf()).toBe(du.valueOf())
expect(du.valueOf()).toBe(mu.valueOf())
expect(d.toDate()).toEqual(du.toDate())
expect(du.toDate()).toEqual(mu.toDate())
expect(du.toISOString()).toEqual(mu.toISOString())
expect(d.toString()).toEqual(d.toString())
})
it('clone', () => {
const du = dayjs().utcOffset(9)
const duClone = du.clone()
expect(du.valueOf()).toBe(duClone.valueOf())
expect(du.format()).toBe(duClone.format())
expect(du.utcOffset()).toBe(duClone.utcOffset())
})
it('immutable', () => {
const d = dayjs()
const du = d.utcOffset(d.utcOffset() + 1)
expect(d.utcOffset()).not.toBe(du.utcOffset())
expect(d.format()).not.toBe(du.format())
})
it('utcOffset(0) enable utc mode', () => {
expect(dayjs().utcOffset(0).format()).toBe(moment().utcOffset(0).format())
expect(dayjs().utcOffset(0).isUTC()).toBeTruthy()
})
it('utcOffset keepLocalTime', () => {
const d = '2000-01-01T06:00:00Z'
expect(dayjs.utc(d).utcOffset(5, true).format())
.toBe(moment.utc(d).utcOffset(5, true).format())
expect(dayjs.utc(d).utcOffset(0, true).format())
.toBe(moment.utc(d).utcOffset(0, true).format())
expect(dayjs.utc(d).utcOffset(-5, true).format())
.toBe(moment.utc(d).utcOffset(-5, true).format())
const d2 = '2016-01-01 00:00:00'
expect(dayjs(d2).utcOffset(0, true).format())
.toBe(moment(d2).utcOffset(0, true).format())
expect(dayjs(d2).utcOffset(-5, true).format())
.toBe(moment(d2).utcOffset(-5, true).format())
expect(dayjs(d2).utcOffset(5, true).format())
.toBe(moment(d2).utcOffset(5, true).format())
})
test('UTC mode', () => {
const d = dayjs.utc('2000-01-01T06:00:00Z')
expect(d.isUTC()).toBeTruthy()
expect(d.utcOffset(0).isUTC()).toBeTruthy()
expect(d.utcOffset(1).isUTC()).toBeFalsy()
})
test('change hours when changing the utc offset in UTC mode', () => {
const d = dayjs.utc('2000-01-01T06:31:00Z')
expect(d.hour()).toBe(6)
expect(d.utcOffset(0).hour()).toBe(6)
expect(d.utcOffset(-60).hour()).toBe(5)
expect(d.utcOffset(60).hour()).toBe(7)
expect(d.utcOffset(-30).format('HH:mm')).toBe('06:01')
expect(d.utcOffset(30).format('HH:mm')).toBe('07:01')
expect(d.utcOffset(-1380).format('HH:mm')).toBe('07:31')
})
test('correctly set and add hours in offset mode', () => {
const d10 = dayjs('2000-01-30T06:31:00+10:00').utcOffset(10)
const dm8 = dayjs('2000-01-30T06:31:00-08:00').utcOffset(-8)
expect(d10.hour(5).hour()).toBe(5)
expect(d10.hour(5).add(1, 'hour').hour()).toBe(6)
expect(d10.hour(5).add(-10, 'hour').hour()).toBe(19)
expect(dm8.hour(5).hour()).toBe(5)
expect(dm8.hour(5).add(1, 'hour').hour()).toBe(6)
expect(dm8.hour(5).add(-10, 'hour').hour()).toBe(19)
})
test('keep hours when adding month in offset mode', () => {
const d10 = dayjs('2000-01-30T06:31:00+10:00').utcOffset(10)
const dm8 = dayjs('2000-01-30T06:31:00-08:00').utcOffset(-8)
expect(d10.add(1, 'month').hour()).toBe(6)
expect(dm8.add(1, 'month').hour()).toBe(6)
expect(d10.add(-2, 'month').hour()).toBe(6)
expect(dm8.add(-2, 'month').hour()).toBe(6)
})
test('utc costrustor', () => {
const d = new Date(2019, 8, 11, 0, 0, 0).getTime()
expect(moment(d).utc().utcOffset(480).valueOf())
.toBe(dayjs(d).utc().utcOffset(480).valueOf())
expect(moment(d).utc().local()
.utcOffset(480)
.valueOf())
.toBe(dayjs(d).utc().local()
.utcOffset(480)
.valueOf())
})
test('utc startOf', () => {
const d = new Date(2019, 8, 11, 0, 0, 0, 0).getTime()
expect(moment(d).utc().utcOffset(480).endOf('day')
.valueOf())
.toBe(dayjs(d).utc().utcOffset(480).endOf('day')
.valueOf())
expect(moment(d).utc().utcOffset(480).endOf('day')
.valueOf())
.toBe(dayjs(d).utc().utcOffset(480).endOf('day')
.valueOf())
const d2 = '2017-07-20T11:00:00+00:00'
const d2d = dayjs(d2).utcOffset(-12).startOf('day').valueOf()
const d2m = moment(d2).utcOffset(-12).startOf('day').valueOf()
expect(d2d)
.toBe(d2m)
expect(d2d)
.toBe(1500465600000)
})
test('cloning dates modified with utcOffset', () => {
const djs = dayjs('2023-10-29T00:00:00+03:00')
const tests = [
djs,
djs.utcOffset(-240),
djs.utcOffset(-240, true),
djs.utcOffset(120),
djs.utcOffset(120, true),
djs.utcOffset(0),
djs.utcOffset(0, true)
]
tests.forEach((d) => {
expect(dayjs(d).format()).toEqual(d.format())
})
})
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/test/plugin/toArray.test.js | test/plugin/toArray.test.js | import MockDate from 'mockdate'
import moment from 'moment'
import dayjs from '../../src'
import toArray from '../../src/plugin/toArray'
dayjs.extend(toArray)
beforeEach(() => {
MockDate.set(new Date())
})
afterEach(() => {
MockDate.reset()
})
it('As Array -> toArray', () => {
expect(dayjs().toArray()).toEqual(moment().toArray())
})
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/test/plugin/badMutable.test.js | test/plugin/badMutable.test.js | import MockDate from 'mockdate'
import moment from 'moment'
import dayjs from '../../src'
import badMutable from '../../src/plugin/badMutable'
import dayOfYear from '../../src/plugin/dayOfYear'
import weekOfYear from '../../src/plugin/weekOfYear'
import '../../src/locale/zh-cn'
dayjs.extend(badMutable)
dayjs.extend(dayOfYear)
dayjs.extend(weekOfYear)
beforeEach(() => {
MockDate.set(new Date())
})
afterEach(() => {
MockDate.reset()
})
describe('Set', () => {
it('Setters', () => {
const d = dayjs()
const m = moment()
expect(d.year()).toBe(m.year())
d.year(2000)
m.year(2000)
expect(d.format()).toBe(m.format())
d.month(1)
m.month(1)
expect(d.format()).toBe(m.format())
d.day(1)
m.day(1)
expect(d.format()).toBe(m.format())
d.date(1)
m.date(1)
expect(d.format()).toBe(m.format())
d.hour(1)
m.hour(1)
expect(d.format()).toBe(m.format())
d.minute(1)
m.minute(1)
expect(d.format()).toBe(m.format())
d.second(1)
m.second(1)
expect(d.format()).toBe(m.format())
d.millisecond(1)
m.millisecond(1)
expect(d.format()).toBe(m.format())
})
it('Set', () => {
const d = dayjs()
const m = moment()
d.set('year', 2000)
m.set('year', 2000)
expect(d.format()).toBe(m.format())
d.set('month', 12)
m.set('month', 12)
expect(d.format()).toBe(m.format())
d.set('day', 1)
m.set('day', 1)
expect(d.format()).toBe(m.format())
d.set('date', 1)
m.set('date', 1)
expect(d.format()).toBe(m.format())
d.set('hour', 1)
m.set('hour', 1)
expect(d.format()).toBe(m.format())
d.set('minute', 1)
m.set('minute', 1)
expect(d.format()).toBe(m.format())
d.set('second', 1)
m.set('second', 1)
expect(d.format()).toBe(m.format())
d.set('millisecond', 1)
m.set('millisecond', 1)
expect(d.format()).toBe(m.format())
})
})
describe('StartOf', () => {
it('StartOf', () => {
const d = dayjs()
const m = moment()
d.startOf('year')
m.startOf('year')
expect(d.format()).toBe(m.format())
d.startOf('month')
m.startOf('month')
expect(d.format()).toBe(m.format())
d.startOf('day')
m.startOf('day')
expect(d.format()).toBe(m.format())
d.startOf('date')
m.startOf('date')
expect(d.format()).toBe(m.format())
d.startOf('hour')
m.startOf('hour')
expect(d.format()).toBe(m.format())
d.startOf('minute')
m.startOf('minute')
expect(d.format()).toBe(m.format())
d.startOf('second')
m.startOf('second')
expect(d.format()).toBe(m.format())
d.startOf('millisecond')
m.startOf('millisecond')
expect(d.format()).toBe(m.format())
d.startOf('week')
m.startOf('week')
expect(d.format()).toBe(m.format())
})
})
describe('Add', () => {
it('Add', () => {
const d = dayjs()
const m = moment()
d.add(1, 'year')
m.add(1, 'year')
expect(d.format()).toBe(m.format())
d.add(12, 'month')
m.add(12, 'month')
expect(d.format()).toBe(m.format())
d.add(1, 'day')
m.add(1, 'day')
expect(d.format()).toBe(m.format())
d.add(1, 'date')
m.add(1, 'date')
expect(d.format()).toBe(m.format())
d.add(1, 'hour')
m.add(1, 'hour')
expect(d.format()).toBe(m.format())
d.add(1, 'minute')
m.add(1, 'minute')
expect(d.format()).toBe(m.format())
d.add(1, 'second')
m.add(1, 'second')
expect(d.format()).toBe(m.format())
d.add(1, 'millisecond')
m.add(1, 'millisecond')
expect(d.format()).toBe(m.format())
d.add(1, 'week')
m.add(1, 'week')
expect(d.format()).toBe(m.format())
})
})
it('daysInMonth', () => {
const d = dayjs()
const m = moment()
expect(d.daysInMonth()).toBe(m.daysInMonth())
expect(d.format()).toBe(m.format())
})
it('Locale', () => {
const d = dayjs()
const m = moment()
const format = 'MMMM'
expect(d.locale()).toBe(m.locale())
expect(d.format(format)).toBe(m.format(format))
d.locale('zh-cn')
m.locale('zh-cn')
expect(d.locale()).toBe(m.locale())
expect(d.format(format)).toBe(m.format(format))
})
it('Diff', () => {
const d = dayjs()
const m = moment()
const unit = 'year'
const d2 = d.clone().add(1, unit)
const m2 = m.clone().add(1, unit)
expect(d.diff(d2, unit)).toBe(-1)
expect(m.diff(m2, unit)).toBe(-1)
})
it('isAfter isBefore isSame', () => {
const d = dayjs()
const format = dayjs().format()
d.isSame(dayjs, 'year')
expect(d.format()).toBe(format)
expect(d.isSame()).toBe(true)
d.isBefore(dayjs, 'hour')
expect(d.format()).toBe(format)
expect(d.isBefore()).toBe(false)
d.isAfter(dayjs, 'month')
expect(d.format()).toBe(format)
expect(d.isAfter()).toBe(false)
})
it('DayOfYear get day won\'t change instance', () => {
const d = dayjs()
const format = d.format()
d.dayOfYear()
expect(d.format()).toBe(format)
})
it('WeekOfYear get week won\'t change instance', () => {
const d = dayjs()
const format = d.format()
d.week()
expect(d.format()).toBe(format)
})
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/test/plugin/negativeYear.test.js | test/plugin/negativeYear.test.js | import MockDate from 'mockdate'
import dayjs from 'dayjs'
import negativeYear from '../../src/plugin/negativeYear'
import utc from '../../src/plugin/utc'
import { REGEX_PARSE } from '../../src/constant'
dayjs.extend(negativeYear)
dayjs.extend(utc)
beforeEach(() => {
MockDate.set(new Date())
})
afterEach(() => {
MockDate.reset()
})
describe('negativeYear', () => {
it('parses negative years', () => {
expect(dayjs('-2020-01-01').year()).toBe(-2020)
const date = '-2021/01/03'
const date2 = '01/03/-2021'
const date3 = '01-03--2021'
const date4 = '-03-15'
const d = date.match(REGEX_PARSE)
expect(dayjs(date).format('YYYY-MM-DD')).toBe('-2021-01-03')
expect(dayjs(date2).format('YYYY-MM-DD')).toBe('Invalid Date')
expect(dayjs(date3).format()).toBe('Invalid Date')
expect(dayjs(date4).format('YYYY-MM-DD')).toBe('2001-03-15')
expect(d).toBe(null)
})
it('does not parse non-negative years', () => {
expect(dayjs('2020-01-01').year()).toBe(2020)
})
it('works with other plugins', () => {
expect(dayjs.utc('-2020-01-01').year()).toBe(-2020)
})
it('Add and subtract with negative years', () => {
expect(dayjs('-2006').add(1, 'y')).toEqual(dayjs('-2005'))
expect(dayjs('-2006').subtract(1, 'y')).toEqual(dayjs('-2007'))
expect(dayjs('-2006').add(1, 'y').format('YYYY')).toBe(dayjs('-2005').format('YYYY'))
expect(dayjs('-2006').subtract(1, 'y').format('YYYY')).toBe(dayjs('-2007').format('YYYY'))
})
it('Compare date with negative years', () => {
expect(dayjs('-2006').isAfter(dayjs('-2007'))).toBeTruthy()
expect(dayjs('-2006').isBefore(dayjs('-2005'))).toBeTruthy()
expect(dayjs('-2006').isSame('-2006')).toBeTruthy()
})
})
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/test/plugin/isoWeeksInYear.test.js | test/plugin/isoWeeksInYear.test.js | import MockDate from 'mockdate'
import dayjs from '../../src'
import isoWeeksInYear from '../../src/plugin/isoWeeksInYear'
import isLeapYear from '../../src/plugin/isLeapYear'
dayjs.extend(isoWeeksInYear)
dayjs.extend(isLeapYear)
beforeEach(() => {
MockDate.set(new Date())
})
afterEach(() => {
MockDate.reset()
})
it('isoWeeksInYear', () => {
expect(dayjs('2004').isoWeeksInYear()).toBe(53)
expect(dayjs('2005').isoWeeksInYear()).toBe(52)
expect(dayjs('2006').isoWeeksInYear()).toBe(52)
expect(dayjs('2007').isoWeeksInYear()).toBe(52)
expect(dayjs('2008').isoWeeksInYear()).toBe(52)
expect(dayjs('2009').isoWeeksInYear()).toBe(53)
expect(dayjs('2010').isoWeeksInYear()).toBe(52)
expect(dayjs('2011').isoWeeksInYear()).toBe(52)
expect(dayjs('2012').isoWeeksInYear()).toBe(52)
expect(dayjs('2013').isoWeeksInYear()).toBe(52)
expect(dayjs('2014').isoWeeksInYear()).toBe(52)
expect(dayjs('2015').isoWeeksInYear()).toBe(53)
})
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/test/plugin/isSameOrBefore.test.js | test/plugin/isSameOrBefore.test.js | import MockDate from 'mockdate'
import dayjs from '../../src'
import isSameOrBefore from '../../src/plugin/isSameOrBefore'
dayjs.extend(isSameOrBefore)
beforeEach(() => {
MockDate.set(new Date())
})
afterEach(() => {
MockDate.reset()
})
test('is same or before without units', () => {
const m = dayjs(new Date(2011, 3, 2, 3, 4, 5, 10))
const mCopy = dayjs(m)
expect(m.isSameOrBefore(dayjs(new Date(2012, 3, 2, 3, 5, 5, 10)))).toBe(true, 'year is later')
expect(m.isSameOrBefore(dayjs(new Date(2010, 3, 2, 3, 3, 5, 10)))).toBe(false, 'year is earlier')
expect(m.isSameOrBefore(dayjs(new Date(2011, 4, 2, 3, 4, 5, 10)))).toBe(true, 'month is later')
expect(m.isSameOrBefore(dayjs(new Date(2011, 2, 2, 3, 4, 5, 10)))).toBe(false, 'month is earlier')
expect(m.isSameOrBefore(dayjs(new Date(2011, 3, 3, 3, 4, 5, 10)))).toBe(true, 'day is later')
expect(m.isSameOrBefore(dayjs(new Date(2011, 3, 1, 3, 4, 5, 10)))).toBe(false, 'day is earlier')
expect(m.isSameOrBefore(dayjs(new Date(2011, 3, 2, 4, 4, 5, 10)))).toBe(true, 'hour is later')
expect(m.isSameOrBefore(dayjs(new Date(2011, 3, 2, 2, 4, 5, 10)))).toBe(false, 'hour is earlier')
expect(m.isSameOrBefore(dayjs(new Date(2011, 3, 2, 3, 5, 5, 10)))).toBe(true, 'minute is later')
expect(m.isSameOrBefore(dayjs(new Date(2011, 3, 2, 3, 3, 5, 10)))).toBe(false, 'minute is earlier')
expect(m.isSameOrBefore(dayjs(new Date(2011, 3, 2, 3, 4, 6, 10)))).toBe(true, 'second is later')
expect(m.isSameOrBefore(dayjs(new Date(2011, 3, 2, 3, 4, 4, 11)))).toBe(false, 'second is earlier')
expect(m.isSameOrBefore(dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)))).toBe(true, 'millisecond match')
expect(m.isSameOrBefore(dayjs(new Date(2011, 3, 2, 3, 4, 5, 11)))).toBe(true, 'millisecond is later')
expect(m.isSameOrBefore(dayjs(new Date(2011, 3, 2, 3, 4, 5, 9)))).toBe(false, 'millisecond is earlier')
expect(m.isSameOrBefore(m)).toBe(true, 'moments are the same as themselves')
expect(+m).toEqual(+mCopy, 'isSameOrBefore second should not change moment')
})
test('is same or before without date', () => {
const past = dayjs().subtract(1, 'day')
const future = dayjs().add(1, 'day')
expect(past.isSameOrBefore()).toBe(true, 'past is before now')
expect(future.isSameOrBefore()).toBe(false, 'future is not before now')
})
test('is same or before year', () => {
const m = dayjs(new Date(2011, 1, 2, 3, 4, 5, 6))
const mCopy = dayjs(m)
expect(m.isSameOrBefore(dayjs(new Date(2011, 5, 6, 7, 8, 9, 10)), 'year')).toBe(true, 'year match')
expect(m.isSameOrBefore(dayjs(new Date(2011, 5, 6, 7, 8, 9, 10)), 'years')).toBe(true, 'plural should work')
expect(m.isSameOrBefore(dayjs(new Date(2012, 5, 6, 7, 8, 9, 10)), 'year')).toBe(true, 'year is later')
expect(m.isSameOrBefore(dayjs(new Date(2010, 5, 6, 7, 8, 9, 10)), 'year')).toBe(false, 'year is earlier')
expect(m.isSameOrBefore(dayjs(new Date(2011, 0, 1, 0, 0, 0, 0)), 'year')).toBe(true, 'exact start of year')
expect(m.isSameOrBefore(dayjs(new Date(2011, 11, 31, 23, 59, 59, 999)), 'year')).toBe(true, 'exact end of year')
expect(m.isSameOrBefore(dayjs(new Date(2012, 0, 1, 0, 0, 0, 0)), 'year')).toBe(true, 'start of next year')
expect(m.isSameOrBefore(dayjs(new Date(2010, 11, 31, 23, 59, 59, 999)), 'year')).toBe(false, 'end of previous year')
expect(m.isSameOrBefore(m, 'year')).toBe(true, 'same moments are in the same year')
expect(+m).toEqual(+mCopy, 'isSameOrBefore year should not change moment')
})
test('is same or before month', () => {
const m = dayjs(new Date(2011, 2, 3, 4, 5, 6, 7))
const mCopy = dayjs(m)
expect(m.isSameOrBefore(dayjs(new Date(2011, 2, 6, 7, 8, 9, 10)), 'month')).toBe(true, 'month match')
expect(m.isSameOrBefore(dayjs(new Date(2011, 2, 6, 7, 8, 9, 10)), 'months')).toBe(true, 'plural should work')
expect(m.isSameOrBefore(dayjs(new Date(2012, 2, 6, 7, 8, 9, 10)), 'month')).toBe(true, 'year is later')
expect(m.isSameOrBefore(dayjs(new Date(2010, 2, 6, 7, 8, 9, 10)), 'month')).toBe(false, 'year is earlier')
expect(m.isSameOrBefore(dayjs(new Date(2011, 5, 6, 7, 8, 9, 10)), 'month')).toBe(true, 'month is later')
expect(m.isSameOrBefore(dayjs(new Date(2011, 1, 6, 7, 8, 9, 10)), 'month')).toBe(false, 'month is earlier')
expect(m.isSameOrBefore(dayjs(new Date(2011, 2, 1, 0, 0, 0, 0)), 'month')).toBe(true, 'exact start of month')
expect(m.isSameOrBefore(dayjs(new Date(2011, 2, 31, 23, 59, 59, 999)), 'month')).toBe(true, 'exact end of month')
expect(m.isSameOrBefore(dayjs(new Date(2011, 3, 1, 0, 0, 0, 0)), 'month')).toBe(true, 'start of next month')
expect(m.isSameOrBefore(dayjs(new Date(2011, 1, 27, 23, 59, 59, 999)), 'month')).toBe(false, 'end of previous month')
expect(m.isSameOrBefore(m, 'month')).toBe(true, 'same moments are in the same month')
expect(+m).toEqual(+mCopy, 'isSameOrBefore month should not change moment')
})
test('is same or before day', () => {
const m = dayjs(new Date(2011, 1, 2, 3, 4, 5, 6))
const mCopy = dayjs(m)
expect(m.isSameOrBefore(dayjs(new Date(2011, 1, 2, 7, 8, 9, 10)), 'day')).toBe(true, 'day match')
expect(m.isSameOrBefore(dayjs(new Date(2011, 1, 2, 7, 8, 9, 10)), 'days')).toBe(true, 'plural should work')
expect(m.isSameOrBefore(dayjs(new Date(2012, 1, 2, 7, 8, 9, 10)), 'day')).toBe(true, 'year is later')
expect(m.isSameOrBefore(dayjs(new Date(2010, 1, 2, 7, 8, 9, 10)), 'day')).toBe(false, 'year is earlier')
expect(m.isSameOrBefore(dayjs(new Date(2011, 2, 2, 7, 8, 9, 10)), 'day')).toBe(true, 'month is later')
expect(m.isSameOrBefore(dayjs(new Date(2010, 12, 2, 7, 8, 9, 10)), 'day')).toBe(false, 'month is earlier')
expect(m.isSameOrBefore(dayjs(new Date(2011, 1, 3, 7, 8, 9, 10)), 'day')).toBe(true, 'day is later')
expect(m.isSameOrBefore(dayjs(new Date(2011, 1, 1, 7, 8, 9, 10)), 'day')).toBe(false, 'day is earlier')
expect(m.isSameOrBefore(dayjs(new Date(2011, 1, 2, 0, 0, 0, 0)), 'day')).toBe(true, 'exact start of day')
expect(m.isSameOrBefore(dayjs(new Date(2011, 1, 2, 23, 59, 59, 999)), 'day')).toBe(true, 'exact end of day')
expect(m.isSameOrBefore(dayjs(new Date(2011, 1, 3, 0, 0, 0, 0)), 'day')).toBe(true, 'start of next day')
expect(m.isSameOrBefore(dayjs(new Date(2011, 1, 1, 23, 59, 59, 999)), 'day')).toBe(false, 'end of previous day')
expect(m.isSameOrBefore(m, 'day')).toBe(true, 'same moments are in the same day')
expect(+m).toEqual(+mCopy, 'isSameOrBefore day should not change moment')
})
test('is same or before hour', () => {
const m = dayjs(new Date(2011, 1, 2, 3, 4, 5, 6))
const mCopy = dayjs(m)
expect(m.isSameOrBefore(dayjs(new Date(2011, 1, 2, 3, 8, 9, 10)), 'hour')).toBe(true, 'hour match')
expect(m.isSameOrBefore(dayjs(new Date(2011, 1, 2, 3, 8, 9, 10)), 'hours')).toBe(true, 'plural should work')
expect(m.isSameOrBefore(dayjs(new Date(2012, 1, 2, 3, 8, 9, 10)), 'hour')).toBe(true, 'year is later')
expect(m.isSameOrBefore(dayjs(new Date(2010, 1, 2, 3, 8, 9, 10)), 'hour')).toBe(false, 'year is earlier')
expect(m.isSameOrBefore(dayjs(new Date(2011, 2, 2, 3, 8, 9, 10)), 'hour')).toBe(true, 'month is later')
expect(m.isSameOrBefore(dayjs(new Date(2010, 12, 2, 3, 8, 9, 10)), 'hour')).toBe(false, 'month is earlier')
expect(m.isSameOrBefore(dayjs(new Date(2011, 1, 3, 3, 8, 9, 10)), 'hour')).toBe(true, 'day is later')
expect(m.isSameOrBefore(dayjs(new Date(2011, 1, 1, 3, 8, 9, 10)), 'hour')).toBe(false, 'day is earlier')
expect(m.isSameOrBefore(dayjs(new Date(2011, 1, 2, 4, 8, 9, 10)), 'hour')).toBe(true, 'hour is later')
expect(m.isSameOrBefore(dayjs(new Date(2011, 1, 2, 2, 8, 9, 10)), 'hour')).toBe(false, 'hour is earlier')
expect(m.isSameOrBefore(dayjs(new Date(2011, 1, 2, 3, 0, 0, 0)), 'hour')).toBe(true, 'exact start of hour')
expect(m.isSameOrBefore(dayjs(new Date(2011, 1, 2, 3, 59, 59, 999)), 'hour')).toBe(true, 'exact end of hour')
expect(m.isSameOrBefore(dayjs(new Date(2011, 1, 2, 4, 0, 0, 0)), 'hour')).toBe(true, 'start of next hour')
expect(m.isSameOrBefore(dayjs(new Date(2011, 1, 2, 2, 59, 59, 999)), 'hour')).toBe(false, 'end of previous hour')
expect(m.isSameOrBefore(m, 'hour')).toBe(true, 'same moments are in the same hour')
expect(+m).toEqual(+mCopy, 'isSameOrBefore hour should not change moment')
})
test('is same or before minute', () => {
const m = dayjs(new Date(2011, 1, 2, 3, 4, 5, 6))
const mCopy = dayjs(m)
expect(m.isSameOrBefore(dayjs(new Date(2011, 1, 2, 3, 4, 9, 10)), 'minute')).toBe(true, 'minute match')
expect(m.isSameOrBefore(dayjs(new Date(2011, 1, 2, 3, 4, 9, 10)), 'minutes')).toBe(true, 'plural should work')
expect(m.isSameOrBefore(dayjs(new Date(2012, 1, 2, 3, 4, 9, 10)), 'minute')).toBe(true, 'year is later')
expect(m.isSameOrBefore(dayjs(new Date(2010, 1, 2, 3, 4, 9, 10)), 'minute')).toBe(false, 'year is earlier')
expect(m.isSameOrBefore(dayjs(new Date(2011, 2, 2, 3, 4, 9, 10)), 'minute')).toBe(true, 'month is later')
expect(m.isSameOrBefore(dayjs(new Date(2010, 12, 2, 3, 4, 9, 10)), 'minute')).toBe(false, 'month is earlier')
expect(m.isSameOrBefore(dayjs(new Date(2011, 1, 3, 3, 4, 9, 10)), 'minute')).toBe(true, 'day is later')
expect(m.isSameOrBefore(dayjs(new Date(2011, 1, 1, 3, 4, 9, 10)), 'minute')).toBe(false, 'day is earlier')
expect(m.isSameOrBefore(dayjs(new Date(2011, 1, 2, 4, 4, 9, 10)), 'minute')).toBe(true, 'hour is later')
expect(m.isSameOrBefore(dayjs(new Date(2011, 1, 2, 2, 4, 9, 10)), 'minute')).toBe(false, 'hour is earlier')
expect(m.isSameOrBefore(dayjs(new Date(2011, 1, 2, 3, 5, 9, 10)), 'minute')).toBe(true, 'minute is later')
expect(m.isSameOrBefore(dayjs(new Date(2011, 1, 2, 3, 3, 9, 10)), 'minute')).toBe(false, 'minute is earlier')
expect(m.isSameOrBefore(dayjs(new Date(2011, 1, 2, 3, 4, 0, 0)), 'minute')).toBe(true, 'exact start of minute')
expect(m.isSameOrBefore(dayjs(new Date(2011, 1, 2, 3, 4, 59, 999)), 'minute')).toBe(true, 'exact end of minute')
expect(m.isSameOrBefore(dayjs(new Date(2011, 1, 2, 3, 5, 0, 0)), 'minute')).toBe(true, 'start of next minute')
expect(m.isSameOrBefore(dayjs(new Date(2011, 1, 2, 3, 3, 59, 999)), 'minute')).toBe(false, 'end of previous minute')
expect(m.isSameOrBefore(m, 'minute')).toBe(true, 'same moments are in the same minute')
expect(+m).toEqual(+mCopy, 'isSameOrBefore minute should not change moment')
})
test('is same or before second', () => {
const m = dayjs(new Date(2011, 1, 2, 3, 4, 5, 6))
const mCopy = dayjs(m)
expect(m.isSameOrBefore(dayjs(new Date(2011, 1, 2, 3, 4, 5, 10)), 'second')).toBe(true, 'second match')
expect(m.isSameOrBefore(dayjs(new Date(2011, 1, 2, 3, 4, 5, 10)), 'seconds')).toBe(true, 'plural should work')
expect(m.isSameOrBefore(dayjs(new Date(2012, 1, 2, 3, 4, 5, 10)), 'second')).toBe(true, 'year is later')
expect(m.isSameOrBefore(dayjs(new Date(2010, 1, 2, 3, 4, 5, 10)), 'second')).toBe(false, 'year is earlier')
expect(m.isSameOrBefore(dayjs(new Date(2011, 2, 2, 3, 4, 5, 10)), 'second')).toBe(true, 'month is later')
expect(m.isSameOrBefore(dayjs(new Date(2010, 12, 2, 3, 4, 5, 10)), 'second')).toBe(false, 'month is earlier')
expect(m.isSameOrBefore(dayjs(new Date(2011, 1, 3, 3, 4, 5, 10)), 'second')).toBe(true, 'day is later')
expect(m.isSameOrBefore(dayjs(new Date(2011, 1, 1, 3, 4, 5, 10)), 'second')).toBe(false, 'day is earlier')
expect(m.isSameOrBefore(dayjs(new Date(2011, 1, 2, 4, 4, 5, 10)), 'second')).toBe(true, 'hour is later')
expect(m.isSameOrBefore(dayjs(new Date(2011, 1, 2, 2, 4, 5, 10)), 'second')).toBe(false, 'hour is earlier')
expect(m.isSameOrBefore(dayjs(new Date(2011, 1, 2, 3, 5, 5, 10)), 'second')).toBe(true, 'minute is later')
expect(m.isSameOrBefore(dayjs(new Date(2011, 1, 2, 3, 3, 5, 10)), 'second')).toBe(false, 'minute is earlier')
expect(m.isSameOrBefore(dayjs(new Date(2011, 1, 2, 3, 4, 6, 10)), 'second')).toBe(true, 'second is later')
expect(m.isSameOrBefore(dayjs(new Date(2011, 1, 2, 3, 4, 4, 10)), 'second')).toBe(false, 'second is earlier')
expect(m.isSameOrBefore(dayjs(new Date(2011, 1, 2, 3, 4, 5, 0)), 'second')).toBe(true, 'exact start of second')
expect(m.isSameOrBefore(dayjs(new Date(2011, 1, 2, 3, 4, 5, 999)), 'second')).toBe(true, 'exact end of second')
expect(m.isSameOrBefore(dayjs(new Date(2011, 1, 2, 3, 4, 6, 0)), 'second')).toBe(true, 'start of next second')
expect(m.isSameOrBefore(dayjs(new Date(2011, 1, 2, 3, 4, 4, 999)), 'second')).toBe(false, 'end of previous second')
expect(m.isSameOrBefore(m, 'second')).toBe(true, 'same moments are in the same second')
expect(+m).toEqual(+mCopy, 'isSameOrBefore second should not change moment')
})
test('is same or before millisecond', () => {
const m = dayjs(new Date(2011, 3, 2, 3, 4, 5, 10))
const mCopy = dayjs(m)
expect(m.isSameOrBefore(dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)), 'millisecond')).toBe(true, 'millisecond match')
expect(m.isSameOrBefore(dayjs(new Date(2011, 3, 2, 3, 4, 5, 10)), 'milliseconds')).toBe(true, 'plural should work')
expect(m.isSameOrBefore(dayjs(new Date(2012, 3, 2, 3, 4, 5, 10)), 'millisecond')).toBe(true, 'year is later')
expect(m.isSameOrBefore(dayjs(new Date(2010, 3, 2, 3, 4, 5, 10)), 'millisecond')).toBe(false, 'year is earlier')
expect(m.isSameOrBefore(dayjs(new Date(2011, 4, 2, 3, 4, 5, 10)), 'millisecond')).toBe(true, 'month is later')
expect(m.isSameOrBefore(dayjs(new Date(2011, 2, 2, 3, 4, 5, 10)), 'millisecond')).toBe(false, 'month is earlier')
expect(m.isSameOrBefore(dayjs(new Date(2011, 3, 3, 3, 4, 5, 10)), 'millisecond')).toBe(true, 'day is later')
expect(m.isSameOrBefore(dayjs(new Date(2011, 3, 1, 1, 4, 5, 10)), 'millisecond')).toBe(false, 'day is earlier')
expect(m.isSameOrBefore(dayjs(new Date(2011, 3, 2, 4, 4, 5, 10)), 'millisecond')).toBe(true, 'hour is later')
expect(m.isSameOrBefore(dayjs(new Date(2011, 3, 1, 4, 1, 5, 10)), 'millisecond')).toBe(false, 'hour is earlier')
expect(m.isSameOrBefore(dayjs(new Date(2011, 3, 2, 3, 5, 5, 10)), 'millisecond')).toBe(true, 'minute is later')
expect(m.isSameOrBefore(dayjs(new Date(2011, 3, 2, 3, 3, 5, 10)), 'millisecond')).toBe(false, 'minute is earlier')
expect(m.isSameOrBefore(dayjs(new Date(2011, 3, 2, 3, 4, 6, 10)), 'millisecond')).toBe(true, 'second is later')
expect(m.isSameOrBefore(dayjs(new Date(2011, 3, 2, 3, 4, 4, 5)), 'millisecond')).toBe(false, 'second is earlier')
expect(m.isSameOrBefore(dayjs(new Date(2011, 3, 2, 3, 4, 6, 11)), 'millisecond')).toBe(true, 'millisecond is later')
expect(m.isSameOrBefore(dayjs(new Date(2011, 3, 2, 3, 4, 4, 9)), 'millisecond')).toBe(false, 'millisecond is earlier')
expect(m.isSameOrBefore(m, 'millisecond')).toBe(true, 'same moments are in the same millisecond')
expect(+m).toEqual(+mCopy, 'isSameOrBefore millisecond should not change moment')
})
test('is same with invalid moments', () => {
const m = dayjs()
const invalid = dayjs(null)
expect(invalid.isSameOrBefore(invalid)).toBe(false, 'invalid moments are not considered equal')
expect(m.isSameOrBefore(invalid)).toBe(false, 'valid moment is not before invalid moment')
expect(invalid.isSameOrBefore(m)).toBe(false, 'invalid moment is not before valid moment')
expect(m.isSameOrBefore(invalid, 'year')).toBe(false, 'invalid moment year')
expect(m.isSameOrBefore(invalid, 'month')).toBe(false, 'invalid moment month')
expect(m.isSameOrBefore(invalid, 'day')).toBe(false, 'invalid moment day')
expect(m.isSameOrBefore(invalid, 'hour')).toBe(false, 'invalid moment hour')
expect(m.isSameOrBefore(invalid, 'minute')).toBe(false, 'invalid moment minute')
expect(m.isSameOrBefore(invalid, 'second')).toBe(false, 'invalid moment second')
expect(m.isSameOrBefore(invalid, 'milliseconds')).toBe(false, 'invalid moment milliseconds')
})
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/test/plugin/bigIntSupport.test.js | test/plugin/bigIntSupport.test.js | import MockDate from 'mockdate'
import moment from 'moment'
import dayjs from '../../src'
import bigIntSupport from '../../src/plugin/bigIntSupport'
dayjs.extend(bigIntSupport)
beforeEach(() => {
MockDate.set(new Date())
})
afterEach(() => {
MockDate.reset()
})
/* global BigInt */
it('Parse BigInt ts and tsms', () => {
const tsms = 1666310421101
const tsmsBig = BigInt(tsms)
const ts = 1666311003
const tsBig = BigInt(ts)
const momentTsms = moment(tsms)
const momentTs = moment.unix(ts)
expect(dayjs(tsms).valueOf()).toBe(momentTsms.valueOf())
expect(dayjs(tsms).valueOf()).toBe(dayjs(tsmsBig).valueOf())
expect(dayjs.unix(ts).valueOf()).toBe(momentTs.valueOf())
expect(dayjs.unix(tsBig).valueOf()).toBe(dayjs.unix(tsBig).valueOf())
})
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/test/plugin/utc.test.js | test/plugin/utc.test.js | import MockDate from 'mockdate'
import moment from 'moment'
import dayjs from '../../src'
import customParseFormat from '../../src/plugin/customParseFormat'
import utc from '../../src/plugin/utc'
dayjs.extend(utc)
beforeEach(() => {
MockDate.set(new Date())
})
afterEach(() => {
MockDate.reset()
})
describe('UTC Get', () => {
it('UTC Year', () => {
expect(dayjs().utc().year()).toBe(moment().utc().year())
})
it('UTC Month', () => {
expect(dayjs().utc().month()).toBe(moment().utc().month())
})
it('UTC Day of Week', () => {
expect(dayjs().utc().day()).toBe(moment().utc().day())
})
it('UTC Date', () => {
expect(dayjs().utc().date()).toBe(moment().utc().date())
})
it('UTC Hour', () => {
expect(dayjs().utc().hour()).toBe(moment().utc().hour())
})
it('UTC Minute', () => {
expect(dayjs().utc().minute()).toBe(moment().utc().minute())
})
it('UTC Second', () => {
expect(dayjs().utc().second()).toBe(moment().utc().second())
})
it('UTC Millisecond', () => {
expect(dayjs().utc().millisecond()).toBe(moment().utc().millisecond())
})
})
describe('Parse UTC ', () => {
it('Parse Now', () => {
expect(dayjs.utc().format()).toBe(moment.utc().format())
expect(dayjs().utc().format()).toBe(moment().utc().format())
})
it('Parse date string without timezone', () => {
const d = '2018-09-06'
expect(dayjs.utc(d).format()).toEqual(moment.utc(d).format())
expect(dayjs.utc(d).format()).toEqual('2018-09-06T00:00:00Z')
expect(dayjs(d).utc().format()).toEqual(moment(d).utc().format())
const d2 = '2018-09'
expect(dayjs.utc(d2).format()).toEqual(moment.utc(d2).format())
expect(dayjs.utc(d2).format()).toEqual('2018-09-01T00:00:00Z')
expect(dayjs(d2).utc().format()).toEqual(moment(d2).utc().format())
const d3 = '2018'
expect(dayjs.utc(d3).format()).toEqual(moment.utc(d3).format())
expect(dayjs.utc(d3).format()).toEqual('2018-01-01T00:00:00Z')
expect(dayjs(d3).utc().format()).toEqual(moment(d3).utc().format())
})
it('creating with utc with timezone', () => {
const d = '2011-02-02T03:04:05+00:00'
expect(dayjs.utc(d).format()).toEqual(moment.utc(d).format())
const d2 = '2012-01-02T08:20:00+09:00'
expect(dayjs.utc(d2).format()).toEqual(moment.utc(d2).format())
})
it('Parse date string without timezone', () => {
const d = '2017-04-22 19:50:16'
expect(dayjs.utc(d).format()).toEqual('2017-04-22T19:50:16Z')
expect(dayjs.utc(d).format()).toEqual(moment.utc(d).format())
expect(dayjs(d).utc().format()).toBe(moment(d).utc().format())
const d2 = '2012-01-02T08:20:00'
expect(dayjs.utc(d2).format()).toEqual(moment.utc(d2).format())
})
it('Parse date string set utc in config', () => {
const d = '2018-09-06T19:34:28Z'
expect(dayjs(d, { utc: true }).format()).toEqual('2018-09-06T19:34:28Z')
expect(dayjs(d, { utc: true }).format()).toEqual(moment(d).utc().format())
expect(dayjs(d).utc().format()).toEqual('2018-09-06T19:34:28Z')
expect(dayjs(d).utc().format()).toEqual(moment(d).utc().format())
expect(dayjs.utc(d).format()).toEqual('2018-09-06T19:34:28Z')
expect(dayjs.utc(d).format()).toEqual(moment.utc(d).format())
})
it('parses unlimited millisecond in utc', () => {
const date = '2019-03-25T06:41:00.999999999'
const ds = dayjs.utc(date)
const ms = moment.utc(date)
expect(ds.valueOf()).toEqual(ms.valueOf())
expect(ds.millisecond()).toEqual(ms.millisecond())
})
})
it('Clone retains the UTC mode', () => {
const instance = dayjs('2018-09-06').utc()
const another = instance.clone()
expect(another.$u).toBeTruthy()
})
it('UTC mode format tokens', () => {
const d = '2018-09-06T19:34:28.657Z'
const instance = dayjs(d).utc()
const format = 'HH-hh-mm-ss-SSS-Z-ZZ'
expect(instance.format(format)).toBe('19-07-34-28-657-+00:00-+0000')
expect(instance.format(format)).toBe(moment.utc(d).format(format))
})
describe('local', () => {
it('Returns a new instance', () => {
const instance = dayjs.utc('2018-09-06T19:34:28.657Z')
const local = instance.local()
expect(local).not.toBe(instance)
})
it('UTC to local', () => {
const d = '2018-09-06'
expect(dayjs.utc(d).local().format()).toEqual(moment.utc(d).local().format())
})
})
it('StartOf EndOf Year ... in UTC mode', () => {
const testArr = ['year', 'month', 'day', 'date', 'week', 'hour', 'minute', 'second']
testArr.forEach((d) => {
expect(dayjs().utc().startOf(d).format()).toBe(moment().utc().startOf(d).format())
expect(dayjs().utc().endOf(d).format()).toBe(moment().utc().endOf(d).format())
})
})
describe('UTC Set', () => {
it('Set UTC Day', () => {
expect(dayjs().utc().set('date', 30).valueOf()).toBe(moment().utc().set('date', 30).valueOf())
})
it('Set UTC Day of Week', () => {
expect(dayjs().utc().set('day', 0).valueOf()).toBe(moment().utc().set('day', 0).valueOf())
})
it('Set UTC Month', () => {
expect(dayjs().utc().set('month', 11).valueOf()).toBe(moment().utc().set('month', 11).valueOf())
})
it('Set UTC Year', () => {
expect(dayjs().utc().set('year', 2008).valueOf()).toBe(moment().utc().set('year', 2008).valueOf())
})
it('Set UTC Hour', () => {
expect(dayjs().utc().set('hour', 6).valueOf()).toBe(moment().utc().set('hour', 6).valueOf())
})
it('Set UTC Minute', () => {
expect(dayjs().utc().set('minute', 59).valueOf()).toBe(moment().utc().set('minute', 59).valueOf())
})
it('Set UTC Second', () => {
expect(dayjs().utc().set('second', 59).valueOf()).toBe(moment().utc().set('second', 59).valueOf())
})
it('Set UTC Millisecond', () => {
expect(dayjs().utc().set('millisecond', 999).valueOf()).toBe(moment().utc().set('millisecond', 999).valueOf())
})
})
it('isUTC', () => {
expect(dayjs().isUTC()).toBe(false)
expect(dayjs().utc().isUTC()).toBe(true)
expect(dayjs.utc().isUTC()).toBe(true)
})
describe('UTC and local', () => {
const localDay = dayjs(Date.UTC(2011, 1, 2, 3, 4, 5, 6))
const utcDay = localDay.utc()
it('utc', () => {
expect(utcDay.date()).toBe(2)
expect(utcDay.day()).toBe(3)
expect(utcDay.hour()).toBe(3)
})
const localAnainDay = utcDay.local()
it('local', () => {
if (localAnainDay.utcOffset() < -180) {
expect(localAnainDay.date()).toBe(1)
expect(localAnainDay.day()).toBe(2)
} else {
expect(localAnainDay.date()).toBe(2)
expect(localAnainDay.day()).toBe(3)
}
})
const offset = Math.floor(localAnainDay.utcOffset() / 60)
const expected = (24 + 3 + offset) % 24
it('utcOffset', () => {
expect(localAnainDay.hour()).toBe(expected)
expect(dayjs().utc().utcOffset()).toBe(0)
})
})
describe('UTC with customParseFormat', () => {
it('Custom Parse Format', () => {
dayjs.extend(customParseFormat)
const instant = dayjs.utc('2011-02-02 03:04:05', 'YYYY-MM-DD HH:mm:ss')
const momentInstant = moment.utc('2011-02-02 03:04:05', 'YYYY-MM-DD HH:mm:ss')
expect(instant.date()).toBe(2)
expect(instant.hour()).toBe(3)
expect(instant.format()).toBe('2011-02-02T03:04:05Z')
expect(instant.format()).toBe(momentInstant.format())
})
})
describe('UTC Offset', () => {
it('get utcOffset', () => {
expect(dayjs().utcOffset()).toBe(moment().utcOffset())
expect(dayjs().utc().utcOffset()).toBe(moment().utc().utcOffset())
})
it('get utc offset with a number value', () => {
const time = '2021-02-28 19:40:10'
const hoursOffset = -8
const daysJS = dayjs(time).utc().utcOffset(hoursOffset * 60, true)
const momentJS = moment(time).utc(true).utcOffset(hoursOffset, true)
expect(daysJS.toISOString()).toEqual(momentJS.toISOString())
expect(daysJS.utcOffset()).toEqual(hoursOffset * 60)
expect(daysJS.utcOffset()).toEqual(momentJS.utcOffset())
})
it('get utc offset with a negative valid string value, format: HH:mm', () => {
const time = '2021-02-28 19:40:10'
const hoursOffset = -8
const daysJS = dayjs(time).utc().utcOffset(`-0${Math.abs(hoursOffset)}:00`, true)
const momentJS = moment(time).utc(true).utcOffset(`-0${Math.abs(hoursOffset)}:00`, true)
expect(daysJS.toISOString()).toEqual(momentJS.toISOString())
expect(daysJS.utcOffset()).toEqual(hoursOffset * 60)
expect(daysJS.utcOffset()).toEqual(momentJS.utcOffset())
})
it('get utc offset with a positive valid string value, format: HH:mm', () => {
const time = '2021-02-28 19:40:10'
const hoursOffset = 8
const daysJS = dayjs(time).utc().utcOffset(`+0${hoursOffset}:00`, true)
const momentJS = moment(time).utc(true).utcOffset(`+0${hoursOffset}:00`, true)
expect(daysJS.toISOString()).toEqual(momentJS.toISOString())
expect(daysJS.utcOffset()).toEqual(hoursOffset * 60)
expect(daysJS.utcOffset()).toEqual(momentJS.utcOffset())
})
it('get utc offset with a negative valid string value, format: HHmm', () => {
const time = '2021-02-28 19:40:10'
const hoursOffset = -8
const daysJS = dayjs(time).utc().utcOffset(`-0${Math.abs(hoursOffset)}00`, true)
const momentJS = moment(time).utc(true).utcOffset(`-0${Math.abs(hoursOffset)}00`, true)
expect(daysJS.toISOString()).toEqual(momentJS.toISOString())
expect(daysJS.utcOffset()).toEqual(hoursOffset * 60)
expect(daysJS.utcOffset()).toEqual(momentJS.utcOffset())
})
it('get utc offset with a positive valid string value, format: HHmm', () => {
const time = '2021-02-28 19:40:10'
const hoursOffset = 8
const daysJS = dayjs(time).utc().utcOffset(`+0${hoursOffset}00`, true)
const momentJS = moment(time).utc(true).utcOffset(`+0${hoursOffset}00`, true)
expect(daysJS.toISOString()).toEqual(momentJS.toISOString())
expect(daysJS.utcOffset()).toEqual(hoursOffset * 60)
expect(daysJS.utcOffset()).toEqual(momentJS.utcOffset())
})
it('get utc offset with an invalid string value, value: random', () => {
const time = '2021-02-28 19:40:10'
const daysJS = dayjs(time, { utc: true }).utc(true).utcOffset('random')
const momentJS = moment(time).utc(true).utcOffset('random')
expect(daysJS.toISOString()).toEqual(momentJS.toISOString())
expect(daysJS.utcOffset()).toEqual(0)
expect(daysJS.utcOffset()).toEqual(momentJS.utcOffset())
})
it('get utc offset with an invalid string value, value: 0', () => {
const time = '2021-02-28 19:40:10'
const daysJS = dayjs(time, { utc: true }).utc(true).utcOffset('+0000')
const momentJS = moment(time).utc(true).utcOffset('+0000')
expect(daysJS.toISOString()).toEqual(momentJS.toISOString())
expect(daysJS.utcOffset()).toEqual(0)
expect(daysJS.utcOffset()).toEqual(momentJS.utcOffset())
})
})
describe('Diff', () => {
const d1 = '2021-06-07'
const d2 = '2021-06-06'
it('utc.diff(utc)', () => {
[dayjs, moment].forEach((_) => {
expect(_.utc(d1).diff(_.utc(d2), 'days')).toBe(1)
expect(_.utc(d1).diff(_.utc(d2), 'm')).toBe(1440)
})
})
it('default diff', () => {
expect(dayjs().diff()).toBeDefined()
})
it('local.diff(utc)', () => {
expect(dayjs(d1).diff(dayjs.utc(d2), 'days'))
.toBe(moment(d1).diff(moment.utc(d2), 'days'))
expect(dayjs(d1).diff(dayjs.utc(d2), 'm'))
.toBe(moment(d1).diff(moment.utc(d2), 'm'))
})
it('utc.diff(local)', () => {
expect(dayjs.utc(d1).diff(d2, 'days'))
.toBe(moment.utc(d1).diff(d2, 'days'))
expect(dayjs.utc(d1).diff(d2, 'm'))
.toBe(moment.utc(d1).diff(d2, 'm'))
})
})
it('utc keepLocalTime', () => {
const t = '2016-05-03 22:15:01'
const d = dayjs(t).utc(true)
const m = moment(t).utc(true)
const fd = d.format()
const dd = d.toDate()
const vd = d.valueOf()
const fm = m.format()
const dm = m.toDate()
const vm = m.valueOf()
expect(fd).toEqual(fm)
expect(fd).toEqual('2016-05-03T22:15:01Z')
expect(dd).toEqual(dm)
expect(vd).toEqual(vm)
})
it('utc diff undefined edge case', () => {
expect(dayjs().diff(undefined, 'seconds')).toBeDefined()
})
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/test/plugin/weekYear.test.js | test/plugin/weekYear.test.js | import moment from 'moment'
import MockDate from 'mockdate'
import dayjs from '../../src'
import weekYear from '../../src/plugin/weekYear'
import weekOfYear from '../../src/plugin/weekOfYear'
import '../../src/locale/en-gb'
dayjs.extend(weekYear)
dayjs.extend(weekOfYear)
beforeEach(() => {
MockDate.set(new Date())
})
afterEach(() => {
MockDate.reset()
})
it('Week Year', () => {
const daySet = [
['2018-12-01', 2018],
['2018-12-30', 2019],
['2018-12-31', 2019],
['2019-01-01', 2019]
]
daySet.forEach((d) => {
const [day, result] = d
const dResult = dayjs(day).weekYear()
expect(dResult).toBe(result)
expect(dResult).toBe(moment(day).weekYear())
})
})
it('yearStart: 4', () => {
const daySet = [
['2020-12-31', 2020],
['2021-01-01', 2020],
['2021-01-02', 2020],
['2021-01-03', 2020],
['2021-01-04', 2021],
['2021-01-05', 2021]
]
daySet.forEach((d) => {
const [day, result] = d
const dResult = dayjs(day).locale('en-gb').weekYear()
expect(dResult).toBe(result)
expect(dResult).toBe(moment(day).locale('en-gb').weekYear())
})
})
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/test/plugin/relativeTime.test.js | test/plugin/relativeTime.test.js | import MockDate from 'mockdate'
import moment from 'moment'
import dayjs from '../../src'
import * as C from '../../src/constant'
import relativeTime from '../../src/plugin/relativeTime'
import updateLocale from '../../src/plugin/updateLocale'
import utc from '../../src/plugin/utc'
import '../../src/locale/ru'
dayjs.extend(relativeTime)
beforeEach(() => {
MockDate.set(new Date('2018-04-04T16:00:00.000Z'))
})
afterEach(() => {
MockDate.reset()
})
it('Time from X', () => {
const T = [
[0, 'second'], // a few seconds
[1, 'second'], // a few seconds
[44, 'second'], // a few seconds
[44.4, 'second'], // a few seconds
[44.5, 'second'], // a minute
[45, 'second'], // a minute
[1, 'minute'], // a minute
[89, 'second'], // a minute
[89.4, 'second'], // a minute
[89.5, 'second'], // a minute
[90, 'second'], // 2 minutes
[44, 'minute'], // 44 minutes
[44.4, 'minute'],
[44.5, 'minute'],
[45, 'minute'], // an hour
[1, 'hour'], // an hour
[89, 'minute'], // an hour
[89.4, 'minute'],
[89.5, 'minute'],
[90, 'minute'], // 2 hours
[21, 'hour'], // 21 hours
[21.4, 'hour'],
[21.5, 'hour'],
[22, 'hour'], // a day
[1, 'day'], // a day
[35, 'hour'], // a day
[35.4, 'hour'],
[35.5, 'hour'],
[36, 'hour'], // 2 days
[25, 'day'], // 25 days
[26, 'day'], // a month
[1, 'month'], // a month
[45, 'day'], // a month
[47, 'day'], // 2 month
[10, 'month'], // 10 month
[11, 'month'], // a year
[1, 'year'], // a year
[17, 'month'], // a year
[18, 'month'] // 2 years
]
T.forEach((t) => {
expect(dayjs().from(dayjs().add(t[0], t[1]))).toBe(moment().from(moment().add(t[0], t[1])))
})
// withoutSuffix
expect(dayjs().from(dayjs().add(3, 'year'), true)).toBe(moment().from(moment().add(3, 'year'), true))
// past date
expect(dayjs().from(dayjs().subtract(3, 'year'))).toBe(moment().from(moment().subtract(3, 'year')))
})
it('Time from now', () => {
expect(dayjs().fromNow()).toBe(moment().fromNow())
expect(dayjs().fromNow(true)).toBe(moment().fromNow(true))
})
it('Time to now', () => {
expect(dayjs().toNow()).toBe(moment().toNow())
expect(dayjs().toNow(true)).toBe(moment().toNow(true))
})
it('Time to X', () => {
// withoutSuffix
expect(dayjs().to(dayjs().add(3, 'year'), true)).toBe(moment().to(moment().add(3, 'year'), true))
// past date
expect(dayjs().to(dayjs().subtract(3, 'year'))).toBe(moment().to(moment().subtract(3, 'year')))
})
it('Locale Function', () => {
// e.g. in ru locale, m: x minute require additional processing
// and provides as a function instead of a string
const str0 = '2020-01-06 15:53:00'
const str = '2020-01-06 15:52:15'
const result = dayjs(str0).locale('ru').to(str)
expect(result).toEqual(expect.any(String))
})
// https://github.com/iamkun/dayjs/issues/646
it('Time from now with UTC', () => {
dayjs.extend(utc)
expect(dayjs.utc().fromNow()).toBe(moment.utc().fromNow())
const currentTime = new Date()
const currentTimestamp = currentTime.getTime()
const currentTimestampAfter37hrs = currentTimestamp + (37 * 60 * 60 * 1000)
let dutc = dayjs.utc(currentTimestampAfter37hrs)
let mutc = moment.utc(currentTimestampAfter37hrs)
expect(dutc.fromNow()).toBe(mutc.fromNow())
// More precise
const currentTimestampAfter36hrs = currentTimestamp + (36.0001 * 60 * 60 * 1000)
dutc = dayjs.utc(currentTimestampAfter36hrs)
mutc = moment.utc(currentTimestampAfter36hrs)
expect(dutc.fromNow()).toBe(mutc.fromNow())
})
it('Custom thresholds and rounding support', () => {
expect(dayjs().subtract(45, 'm').fromNow()).toBe('an hour ago')
delete relativeTime.$i // this allow plugin to be installed again
dayjs.extend(relativeTime, {
rounding: Math.floor,
thresholds: [
{ l: 's', r: 1 },
{ l: 'm', r: 1 },
{ l: 'mm', r: 59, d: C.MIN },
{ l: 'h', r: 1 },
{ l: 'hh', r: 23, d: C.H },
{ l: 'd', r: 1 },
{ l: 'dd', r: 29, d: C.D },
{ l: 'M', r: 1 },
{ l: 'MM', r: 11, d: C.M },
{ l: 'y' },
{ l: 'yy', d: C.Y }
]
})
expect(dayjs().subtract(45, 'm').fromNow()).toBe('45 minutes ago')
})
it('Locale without relativeTime config fallback', () => {
expect(dayjs().locale({
name: 'test-locale'
}).fromNow()).toEqual(expect.any(String))
})
it('Past and Future keys should support function for additional processing', () => {
dayjs.extend(updateLocale)
dayjs.updateLocale('en', {
relativeTime: {
future: input => `${input} modified`,
past: input => `${input} modified`,
s: 'just now',
m: ' 1 min',
mm: '%d min',
h: '1 hr',
hh: '%d hrs',
d: 'a day',
dd: '%d days',
M: 'a month',
MM: '%d months',
y: 'a year',
yy: '%d years'
}
})
const past = Date.now() - 1000
expect(dayjs(past).fromNow()).toEqual(' 1 min modified')
const future = Date.now() + 1000
expect(dayjs(future).fromNow()).toEqual(' 1 min modified')
})
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/test/plugin/weekOfYear.test.js | test/plugin/weekOfYear.test.js | 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(() => {
MockDate.set(new Date())
})
afterEach(() => {
MockDate.reset()
})
it('Week of year', () => {
dayjs.locale('en')
const day = '2018-12-31T10:59:09+08:00'
const week = 27
expect(dayjs(day).week()).toBe(moment(day).week())
expect(dayjs().week()).toBe(moment().week())
expect(dayjs().week(week).week()).toBe(moment().week(week).week())
expect(dayjs().weeks(week).week()).toBe(moment().weeks(week).week())
expect(dayjs().weeks(-week).week()).toBe(moment().weeks(-week).week())
expect(dayjs().weeks(55).week()).toBe(moment().weeks(55).week())
expect(dayjs().weeks()).toBe(moment().weeks())
})
it('Week of year with locale', () => {
dayjs.locale('en-gb')
moment.locale('en-gb')
const day = '2019-07-28'
expect(dayjs(day).week()).toBe(moment(day).week())
})
describe('Week of year with locale edges', () => {
const testCases = [
'2018-12-30',
'2018-12-31',
'2019-12-29',
'2019-12-30',
'2016-01-01',
'2016-01-04'
]
testCases.forEach((t) => {
it(`Edges ${t}`, () => {
expect(dayjs(t).week())
.toBe(moment(t).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'))
})
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/test/plugin/customParseFormat.test.js | test/plugin/customParseFormat.test.js | import MockDate from 'mockdate'
import moment from 'moment'
import dayjs from '../../src'
import '../../src/locale/ru'
import uk from '../../src/locale/uk'
import '../../src/locale/zh-cn'
import customParseFormat from '../../src/plugin/customParseFormat'
import advancedFormat from '../../src/plugin/advancedFormat'
import localizedFormats from '../../src/plugin/localizedFormat'
import weekOfYear from '../../src/plugin/weekOfYear'
dayjs.extend(customParseFormat)
dayjs.extend(localizedFormats)
dayjs.extend(weekOfYear) // test parse w, ww
beforeEach(() => {
MockDate.set(new Date())
})
afterEach(() => {
MockDate.reset()
})
it('does not break the built-in parsing', () => {
const input = '2018-05-02 01:02:03.004'
expect(dayjs(input).valueOf()).toBe(moment(input).valueOf())
})
it('parse padded string', () => {
const input = '2018-05-02 01:02:03.004 AM +01:00'
const format = 'YYYY-MM-DD HH:mm:ss.SSS A Z'
expect(dayjs(input, format).valueOf()).toBe(moment(input, format).valueOf())
})
it('parse string for MMM month format', () => {
const input = '4/Mar/2019:11:16:26 +0800'
const format = 'D/MMM/YYYY:H:m:s zz'
expect(dayjs(input, format).valueOf()).toBe(moment(input, format).valueOf())
const input2 = '21-Dec-18'
const format2 = 'D-MMM-YY'
expect(dayjs(input2, format2).valueOf()).toBe(moment(input2, format2).valueOf())
})
it('parse string January (getMonth() = 0)', () => {
const input = '01/01/2019'
const format = 'DD/MM/YYYY'
expect(dayjs(input, format).valueOf()).toBe(moment(input, format).valueOf())
})
it('parse unpadded string', () => {
const input = '2.5.18 1:2:3.4 PM -0100'
const format = 'D.M.YY H:m:s.S A ZZ'
expect(dayjs(input, format).valueOf()).toBe(moment(input, format).valueOf())
})
it('parse time zone abbreviation', () => {
const input = '05/02/69 1:02:03.004 AM +01:00 (CET)'
const format = 'MM/DD/YY h:mm:ss.SSS A Z (z)'
expect(dayjs(input, format).valueOf()).toBe(moment(input, format).valueOf())
})
it('parse time zone abbreviation2', () => {
const input = '05/02/69 1:02:03.04 AM +01:00 (CET)'
const format = 'MM/DD/YY h:mm:ss.SS A Z (z)'
expect(dayjs(input, format).valueOf()).toBe(moment(input, format).valueOf())
})
it('recognizes midnight in small letters', () => {
const input = '2018-05-02 12:00 am'
const format = 'YYYY-MM-DD hh:mm a'
expect(dayjs(input, format).valueOf()).toBe(moment(input, format).valueOf())
})
it('recognizes noon in small letters', () => {
const input = '2018-05-02 12:00 pm'
const format = 'YYYY-MM-DD hh:mm a'
expect(dayjs(input, format).valueOf()).toBe(moment(input, format).valueOf())
})
describe('parse localizedFormats', () => {
['zh-cn', 'ru', 'uk', 'en'].forEach((lo) => {
it(`Locale: ${lo}`, () => {
const input = '2018-05-02 01:02:03.004'
dayjs.locale(lo)
moment.locale(lo)
const longDateFormats = ['LT', 'LTS', 'L', 'LL', 'l', 'll', 'lll', 'l LT', 'LL [l] LTS'] // TODO: fix LLL, LLLL and llll
longDateFormats.forEach((f) => {
const localizedInput = moment(input).format(f)
expect(dayjs(localizedInput, f).valueOf()).toBe(moment(localizedInput, f).valueOf())
})
})
})
})
it('leaves non-token parts of the format intact', () => {
const input = '2018-05-02 12:00 +0000 S:/-.() SS h '
const format = 'YYYY-MM-DD HH:mm ZZ [S]:/-.()[ SS h ]'
expect(dayjs(input, format).valueOf()).toBe(moment(input, format).valueOf())
})
it('timezone with no hour', () => {
const input = '2018-05-02 +0000'
const format = 'YYYY-MM-DD ZZ'
expect(dayjs(input, format).valueOf()).toBe(moment(input, format).valueOf())
})
describe('Timezone Offset', () => {
it('timezone with 2-digit offset', () => {
const input = '2020-12-01T20:00:00+09'
const format = 'YYYY-MM-DD[T]HH:mm:ssZZ'
const result = dayjs(input, format)
expect(result.valueOf()).toBe(moment(input, format).valueOf())
expect(result.valueOf()).toBe(1606820400000)
})
it('zulu', () => {
const input = '2021-01-26T15:38:43.000Z'
const format = 'YYYY-MM-DDTHH:mm:ss.SSSZ'
const result = dayjs(input, format)
expect(result.valueOf()).toBe(moment(input, format).valueOf())
expect(result.valueOf()).toBe(1611675523000)
})
it('no timezone format token should parse in local time', () => {
const input = '2020-12-01T20:00:00+01:00'
const format = 'YYYY-MM-DD[T]HH:mm:ss'
const result = dayjs(input, format)
expect(result.valueOf()).toBe(moment(input, format).valueOf())
})
})
it('parse hh:mm', () => {
const input = '12:00'
const format = 'hh:mm'
expect(dayjs(input, format).valueOf()).toBe(moment(input, format).valueOf())
})
it('parse HH:mm:ss', () => {
const input = '00:27:21'
const format = 'HH:mm:ss'
expect(dayjs(input, format).valueOf()).toBe(moment(input, format).valueOf())
})
it('parse HH:mm:ss but only one digit', () => {
const input = '0:0:1'
const format = 'HH:mm:ss'
expect(dayjs(input, format).valueOf()).toBe(moment(input, format).valueOf())
})
describe('parse YYYY / YYYY-MM only', () => {
it('YYYY', () => {
const input = '2001 +08:00'
const format = 'YYYY Z'
expect(dayjs(input, format).valueOf()).toBe(moment(input, format).valueOf())
const input2 = '2001'
const format2 = 'YYYY'
expect(dayjs(input2, format2).valueOf()).toBe(moment(input2, format2).valueOf())
})
it('YYYY-MM', () => {
const input = '2001-01 +08:00'
const format = 'YYYY-MM Z'
expect(dayjs(input, format).valueOf()).toBe(moment(input, format).valueOf())
const input2 = '2001-01'
const format2 = 'YYYY-MM'
expect(dayjs(input2, format2).valueOf()).toBe(moment(input2, format2).valueOf())
})
})
it('parse hh:mm:ss but only one digit', () => {
const input = '0:0:1'
const format = 'hh:mm:ss'
expect(dayjs(input, format).valueOf()).toBe(moment(input, format).valueOf())
})
it('fails with an invalid format', () => {
const input = '2018-05-02 12:00 PM'
const format = 'C'
expect(dayjs(input, format).format().toLowerCase())
.toBe(moment(input, format).format().toLowerCase())
})
it('parse month from string', () => {
const input = '2018 February 03'
const format = 'YYYY MMMM DD'
expect(dayjs(input, format).valueOf()).toBe(moment(input, format).valueOf())
const input2 = '21-December-18'
const format2 = 'D-MMMM-YY'
expect(dayjs(input2, format2).valueOf()).toBe(moment(input2, format2).valueOf())
})
it('parse month from short string', () => {
const input = '2018 Feb 03'
const format = 'YYYY MMM DD'
expect(dayjs(input, format).valueOf()).toBe(moment(input, format).valueOf())
})
it('parse month from string with locale in config', () => {
const input = '2018 лютий 03'
const format = 'YYYY MMMM DD'
expect(dayjs(input, format, 'uk').valueOf()).toBe(moment(input, format, 'uk').valueOf())
})
it('parse month from short string with locale in config', () => {
const input = '2018 трав 03'
const format = 'YYYY MMM DD'
expect(dayjs(input, format, 'uk').valueOf()).toBe(moment(input, format, 'uk').valueOf())
})
it('parse month from short string with locale in argument', () => {
const input = '2018 трав 03'
const format = 'YYYY MMM DD'
expect(dayjs(input, format, 'uk').valueOf()).toBe(moment(input, format, 'uk').valueOf())
})
it('parse month from string with locale in argument', () => {
const input = '2018 лютий 03'
const format = 'YYYY MMMM DD'
expect(dayjs(input, format, 'uk').valueOf()).toBe(moment(input, format, 'uk').valueOf())
})
it('return Invalid Date when parse corrupt string', () => {
const input = '2018 Turnip 03'
const format = 'YYYY MMMM DD'
expect(dayjs(input, format).format()).toBe('Invalid Date')
})
it('return Invalid Date when parse corrupt short string', () => {
const input = '2018 Dog 03'
const format = 'YYYY MMM DD'
expect(dayjs(input, format).format()).toBe('Invalid Date')
})
it('YYYY-MM set 1st day of the month', () => {
expect(dayjs('2019-02', 'YYYY-MM').format('YYYY-MM-DD')).toBe('2019-02-01')
})
it('Invalid Dates', () => {
expect(dayjs('10/12/2014', 'YYYY-MM-DD').format('MM-DD-YYYY')).toBe('Invalid Date')
expect(dayjs('10-12-2014', 'YYYY-MM-DD').format('MM-DD-YYYY')).toBe('Invalid Date')
})
it('Valid Date', () => {
expect(dayjs('2014/10/12', 'YYYY-MM-DD').format('MM-DD-YYYY')).toBe('10-12-2014')
})
it('correctly parse month from string after changing locale globally', () => {
const input = '2018 лютий 03'
const format = 'YYYY MMMM DD'
const dayjsLocale = dayjs().$locale()
const momentLocale = moment.locale()
try {
dayjs.locale(uk)
moment.locale('uk')
expect(dayjs(input, format).valueOf()).toBe(moment(input, format).valueOf())
} finally {
dayjs.locale(dayjsLocale)
moment.locale(momentLocale)
}
})
it('correctly parse ordinal', () => {
const input = '7th March 2019'
const input2 = '17th March 2019'
const inputFalse = '7st March 2019'
const inputZHCN = '7日 三月 2019'
const format = 'Do MMMM YYYY'
const displayFormatWithLocale = 'MMMM dddd'
expect(dayjs(input, format).valueOf())
.toBe(moment(input, format).valueOf())
expect(dayjs(input2, format).valueOf())
.toBe(moment(input2, format).valueOf())
expect(dayjs(inputFalse, format).valueOf())
.toBe(moment(inputFalse, format).valueOf())
const dayjsCN = dayjs(inputZHCN, format, 'zh-cn')
const momentCN = moment(inputZHCN, format, 'zh-cn')
expect(dayjsCN.valueOf())
.toBe(momentCN.valueOf())
expect(dayjsCN.format(displayFormatWithLocale))
.toBe(momentCN.format(displayFormatWithLocale))
expect(dayjsCN.locale())
.toBe(momentCN.locale())
})
describe('month function locale', () => {
it('MMMM', () => {
const input = '08 мая 2020'
const input2 = '08 май 2020'
const format = 'DD MMMM YYYY'
expect(dayjs(input, format, 'ru').valueOf()).toBe(moment(input, format, 'ru').valueOf())
expect(dayjs(input2, format, 'ru').valueOf()).toBe(moment(input2, format, 'ru').valueOf())
})
it('MMM', () => {
const input = '08 февр. 2020'
const format = 'DD MMM YYYY'
expect(dayjs(input, format, 'ru').valueOf()).toBe(moment(input, format, 'ru').valueOf())
})
})
describe('Strict mode', () => {
it('without locale', () => {
const input = '1970-00-00'
const format = 'YYYY-MM-DD'
expect(dayjs(input, format).isValid()).toBe(true)
expect(dayjs(input, format, true).isValid()).toBe(false)
expect(dayjs('2020-Jan-01', 'YYYY-MMM-DD', true).isValid()).toBe(true)
expect(dayjs('30/1/2020 10:59 PM', 'D/M/YYYY h:mm A', true).isValid()).toBe(true)
})
it('with locale', () => {
const input = '2018 三月 99'
const format = 'YYYY MMMM DD'
expect(dayjs(input, format, 'zh-cn').isValid()).toBe(true)
expect(dayjs(input, format, 'zh-cn', true).isValid()).toBe(false)
})
})
describe('Array format support', () => {
it('second ok', () => {
const input = '2012-05-28'
const format = ['YYYY', 'YYYY-MM-DD']
expect(dayjs(input, format).isValid()).toBe(true)
expect(dayjs(input, format, true).format('YYYY-MM-DD')).toBe('2012-05-28')
})
it('all invalid', () => {
const input = '2012-05-28'
const format = ['DD', 'MM-DD']
expect(dayjs(input, format, true).isValid()).toBe(false)
})
it('with locale', () => {
const input = '2018 三月 12'
const format = ['YYYY', 'MM', 'YYYY MMMM DD']
expect(dayjs(input, format, 'zh-cn', true).format('YYYY MMMM DD')).toBe(input)
})
})
describe('meridiem locale', () => {
const format = 'YYYY年M月D日Ah点mm分ss秒'
const format2 = 'YYYY-MM-DD HH:mm:ss'
it('AM', () => {
const input = '2018-05-02 01:02:03'
const date = dayjs(input).locale('zh-cn').format(format)
expect(dayjs(date, format, 'zh-cn').format(format2)).toBe(input)
})
it('PM', () => {
const input = '2018-05-02 20:02:03'
const date = dayjs(input).locale('zh-cn').format(format)
expect(dayjs(date, format, 'zh-cn').format(format2)).toBe(input)
})
})
it('parse a string for MMM month format with underscore delimiter', () => {
const input = 'Jan_2021'
const format = 'MMM_YYYY'
expect(dayjs(input, format).valueOf()).toBe(moment(input, format).valueOf())
const input2 = '21_Jan_2021_123523'
const format2 = 'DD_MMM_YYYY_hhmmss'
expect(dayjs(input2, format2).valueOf()).toBe(moment(input2, format2).valueOf())
})
it('custom two-digit year parse function', () => {
delete customParseFormat.$i // this allow plugin to be installed again
dayjs.extend(customParseFormat, {
parseTwoDigitYear: yearString => (+yearString) + 1800
})
const format = 'YY-MM-DD'
const input = '00-05-02'
expect(dayjs(input, format).year()).toBe(1800)
const input2 = '50-05-02'
expect(dayjs(input2, format).year()).toBe(1850)
const input3 = '99-05-02'
expect(dayjs(input3, format).year()).toBe(1899)
})
// issue 1852
describe('parse with special separator characters', () => {
it('Output is NaN for a specific date format', () => {
const input = '20 Nov, 2022'
const format = 'DD MMM, YYYY'
const locale = 'en'
const resultDayjs = dayjs(input, format, locale)
const resultMoment = moment(input, format, locale)
expect(resultMoment.isValid()).toBe(true)
expect(resultDayjs.isValid()).toBe(true)
expect(resultDayjs.format('DD-MM-YYYY')).toBe('20-11-2022')
expect(resultMoment.format('DD-MM-YYYY')).toBe('20-11-2022')
})
it('parse comma separated date', () => {
const input = '20,11,2022'
const format = 'DD,MM,YYYY'
const resultDayjs = dayjs(input, format)
const resultMoment = moment(input, format)
expect(resultMoment.isValid()).toBe(true)
expect(resultDayjs.isValid()).toBe(true)
expect(resultDayjs.format('DD-MM-YYYY')).toBe('20-11-2022')
expect(resultMoment.format('DD-MM-YYYY')).toBe('20-11-2022')
})
it('parse comma separated date in strict mode', () => {
const input = '20,11,2022'
const format = 'DD,MM,YYYY'
const resultDayjs = dayjs(input, format, true)
const resultMoment = moment(input, format, true)
expect(resultMoment.isValid()).toBe(true)
expect(resultDayjs.isValid()).toBe(true)
expect(resultDayjs.format('DD-MM-YYYY')).toBe('20-11-2022')
expect(resultMoment.format('DD-MM-YYYY')).toBe('20-11-2022')
})
it('parse date with multi character separator', () => {
const input = '20---11---2022'
const format = 'DD-/-MM-#-YYYY'
const resultDayjs = dayjs(input, format)
const resultMoment = moment(input, format)
expect(resultMoment.isValid()).toBe(true)
expect(resultDayjs.isValid()).toBe(true)
expect(resultDayjs.format('DD-MM-YYYY')).toBe('20-11-2022')
expect(resultMoment.format('DD-MM-YYYY')).toBe('20-11-2022')
})
it('parse date with multi character separator in strict mode', () => {
const input = '20-/-11-#-2022'
const format = 'DD-/-MM-#-YYYY'
const resultDayjs = dayjs(input, format, true)
const resultMoment = moment(input, format, true)
expect(resultMoment.isValid()).toBe(true)
expect(resultDayjs.isValid()).toBe(true)
expect(resultDayjs.format('DD-MM-YYYY')).toBe('20-11-2022')
expect(resultMoment.format('DD-MM-YYYY')).toBe('20-11-2022')
})
})
it('parse X x', () => {
const input = '1410715640.579'
const format = 'X'
expect(dayjs(input, format).valueOf()).toBe(moment(input, format).valueOf())
const input2 = '1410715640579'
const format2 = 'x'
expect(dayjs(input2, format2).valueOf()).toBe(moment(input2, format2).valueOf())
// x X starct parse requires advancedFormat plugin
dayjs.extend(advancedFormat)
expect(dayjs(input2, format2, true).valueOf()).toBe(moment(input2, format2, true).valueOf())
})
it('parse Q, [Q]', () => {
const input1 = '2024-Q1'
const input2 = '2024-Q2'
const input3 = '2024-Q3'
const input4 = '2024-Q4'
const format = 'YYYY-[Q]Q'
expect(dayjs(input1, format).valueOf()).toBe(moment(input1, format).valueOf())
expect(dayjs(input2, format).valueOf()).toBe(moment(input2, format).valueOf())
expect(dayjs(input3, format).valueOf()).toBe(moment(input3, format).valueOf())
expect(dayjs(input4, format).valueOf()).toBe(moment(input4, format).valueOf())
})
it('parse w, ww', () => {
const input = '2024-w1'
const format1 = 'YYYY-[w]w'
expect(dayjs(input, format1).format(format1)).toBe(input)
const input2 = '2024-w32'
const format2 = 'YYYY-[w]ww'
expect(dayjs(input2, format2).format(format1)).toBe(input2)
})
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/test/plugin/isoWeek.test.js | test/plugin/isoWeek.test.js | import MockDate from 'mockdate'
import moment from 'moment'
import dayjs from '../../src'
import isoWeek from '../../src/plugin/isoWeek'
import utc from '../../src/plugin/utc'
dayjs.extend(isoWeek)
dayjs.extend(utc)
beforeEach(() => {
MockDate.set(new Date())
})
afterEach(() => {
MockDate.reset()
})
it('get isoWeek', () => {
expect(dayjs().isoWeek()).toBe(moment().isoWeek())
})
it('set isoWeek', () => {
expect(dayjs().isoWeek(1).valueOf()).toBe(moment().isoWeek(1).valueOf())
expect(dayjs().isoWeek(52).valueOf()).toBe(moment().isoWeek(52).valueOf())
})
it('get isoWeekYear', () => {
expect(dayjs().isoWeekYear()).toBe(moment().isoWeekYear())
})
it('startOf/endOf isoWeek', () => {
const ISOWEEK = 'isoWeek'
expect(dayjs().startOf(ISOWEEK).valueOf()).toBe(moment().startOf(ISOWEEK).valueOf())
expect(dayjs().endOf(ISOWEEK).valueOf()).toBe(moment().endOf(ISOWEEK).valueOf())
})
it('isoWeekday', () => {
expect(dayjs().isoWeekday()).toBe(moment().isoWeekday())
expect(dayjs('20200301').isoWeekday(1).valueOf()).toBe(moment('20200301').isoWeekday(1).valueOf()) // Sunday this.day() -> 0
for (let i = 0; i < 7; i += 1) {
expect(dayjs().add(i, 'day').isoWeekday()).toBe(moment().add(i, 'day').isoWeekday())
expect(dayjs().isoWeekday(i).valueOf()).toBe(moment().isoWeekday(i).valueOf())
expect(dayjs().add(1, 'day').isoWeekday(i).valueOf()).toBe(moment().add(1, 'day').isoWeekday(i).valueOf())
}
})
it('isoWeek of year', () => {
expect(dayjs().isoWeek(1).isoWeek()).toBe(1)
expect(dayjs().isoWeek(27).isoWeek()).toBe(27)
expect(dayjs('20191223').isoWeekYear()).toBe(2019)
expect(dayjs('20191223').isoWeek()).toBe(52)
expect(dayjs('20191224').isoWeekYear()).toBe(2019)
expect(dayjs('20191224').isoWeek()).toBe(52)
expect(dayjs('20191225').isoWeekYear()).toBe(2019)
expect(dayjs('20191225').isoWeek()).toBe(52)
expect(dayjs('20191226').isoWeekYear()).toBe(2019)
expect(dayjs('20191226').isoWeek()).toBe(52)
expect(dayjs('20191227').isoWeekYear()).toBe(2019)
expect(dayjs('20191227').isoWeek()).toBe(52)
expect(dayjs('20191228').isoWeekYear()).toBe(2019)
expect(dayjs('20191228').isoWeek()).toBe(52)
expect(dayjs('20191229').isoWeekYear()).toBe(2019)
expect(dayjs('20191229').isoWeek()).toBe(52)
expect(dayjs('20191230').isoWeekYear()).toBe(2020)
expect(dayjs('20191230').isoWeek()).toBe(1)
expect(dayjs('20191231').isoWeekYear()).toBe(2020)
expect(dayjs('20191231').isoWeek()).toBe(1)
expect(dayjs('20200101').isoWeekYear()).toBe(2020)
expect(dayjs('20200101').isoWeek()).toBe(1)
expect(dayjs('20200102').isoWeekYear()).toBe(2020)
expect(dayjs('20200102').isoWeek()).toBe(1)
expect(dayjs('20200103').isoWeekYear()).toBe(2020)
expect(dayjs('20200103').isoWeek()).toBe(1)
expect(dayjs('20200104').isoWeekYear()).toBe(2020)
expect(dayjs('20200104').isoWeek()).toBe(1)
expect(dayjs('20200105').isoWeekYear()).toBe(2020)
expect(dayjs('20200105').isoWeek()).toBe(1)
expect(dayjs('20200106').isoWeekYear()).toBe(2020)
expect(dayjs('20200106').isoWeek()).toBe(2)
expect(dayjs('20200107').isoWeekYear()).toBe(2020)
expect(dayjs('20200107').isoWeek()).toBe(2)
expect(dayjs('20201223').isoWeekYear()).toBe(2020)
expect(dayjs('20201223').isoWeek()).toBe(52)
expect(dayjs('20201224').isoWeekYear()).toBe(2020)
expect(dayjs('20201224').isoWeek()).toBe(52)
expect(dayjs('20201225').isoWeekYear()).toBe(2020)
expect(dayjs('20201225').isoWeek()).toBe(52)
expect(dayjs('20201226').isoWeekYear()).toBe(2020)
expect(dayjs('20201226').isoWeek()).toBe(52)
expect(dayjs('20201227').isoWeekYear()).toBe(2020)
expect(dayjs('20201227').isoWeek()).toBe(52)
expect(dayjs('20201228').isoWeekYear()).toBe(2020)
expect(dayjs('20201228').isoWeek()).toBe(53)
expect(dayjs('20201229').isoWeekYear()).toBe(2020)
expect(dayjs('20201229').isoWeek()).toBe(53)
expect(dayjs('20201230').isoWeekYear()).toBe(2020)
expect(dayjs('20201230').isoWeek()).toBe(53)
expect(dayjs('20201231').isoWeekYear()).toBe(2020)
expect(dayjs('20201231').isoWeek()).toBe(53)
expect(dayjs('20210101').isoWeekYear()).toBe(2020)
expect(dayjs('20210101').isoWeek()).toBe(53)
expect(dayjs('20210102').isoWeekYear()).toBe(2020)
expect(dayjs('20210102').isoWeek()).toBe(53)
expect(dayjs('20210103').isoWeekYear()).toBe(2020)
expect(dayjs('20210103').isoWeek()).toBe(53)
expect(dayjs('20210104').isoWeekYear()).toBe(2021)
expect(dayjs('20210104').isoWeek()).toBe(1)
expect(dayjs('20210105').isoWeekYear()).toBe(2021)
expect(dayjs('20210105').isoWeek()).toBe(1)
expect(dayjs('20210106').isoWeekYear()).toBe(2021)
expect(dayjs('20210106').isoWeek()).toBe(1)
expect(dayjs('20210107').isoWeekYear()).toBe(2021)
expect(dayjs('20210107').isoWeek()).toBe(1)
expect(dayjs('20210108').isoWeekYear()).toBe(2021)
expect(dayjs('20210108').isoWeek()).toBe(1)
expect(dayjs('20210109').isoWeekYear()).toBe(2021)
expect(dayjs('20210109').isoWeek()).toBe(1)
expect(dayjs('20210110').isoWeekYear()).toBe(2021)
expect(dayjs('20210110').isoWeek()).toBe(1)
})
it('utc mode', () => {
// Wednesday, 1 January 2020 00:00:00 UTC
const d = dayjs.utc(1577836800000).isoWeek()
expect(d).toBe(1)
expect(moment.utc(1577836800000).isoWeek()).toBe(d)
})
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/test/plugin/updateLocale.test.js | test/plugin/updateLocale.test.js | import MockDate from 'mockdate'
import moment from 'moment'
import dayjs from '../../src'
import updateLocale from '../../src/plugin/updateLocale'
import localizedFormat from '../../src/plugin/localizedFormat'
import '../../src/locale/zh-cn'
dayjs.extend(updateLocale)
dayjs.extend(localizedFormat)
beforeEach(() => {
MockDate.set(new Date())
})
afterEach(() => {
MockDate.reset()
})
const newLocale = {
months: new Array(12).fill('testMonth'),
formats: { // formats for dayjs and longDateFormat for momentjs
LT: '[testFormat]'
},
longDateFormat: {
LT: '[testFormat]'
}
}
const formatString = 'MMMM LT'
describe('Update locale', () => {
it('Invalid argument', () => {
const result = dayjs.updateLocale('InvalidLocaleName', {})
expect(result)
.toEqual(undefined)
expect(dayjs().format(formatString))
.toEqual(moment().format(formatString))
})
it('Return value', () => {
const result1 = dayjs.updateLocale('en')
expect(typeof result1).toEqual('object')
const result2 = dayjs.updateLocale('en', {})
expect(typeof result2).toEqual('object')
const result3 = dayjs.updateLocale('en', newLocale)
expect(typeof result3).toEqual('object')
})
it('Update build-in en locale', () => {
moment.updateLocale('en', newLocale)
dayjs.updateLocale('en', newLocale)
expect(dayjs().format(formatString))
.toEqual('testMonth testFormat')
expect(dayjs().format(formatString))
.toEqual(moment().format(formatString))
})
it('Update imported zh-cn locale', () => {
moment.updateLocale('zh-cn', newLocale)
dayjs.updateLocale('zh-cn', newLocale)
dayjs.locale('zh-cn')
moment.locale('zh-cn')
expect(dayjs().format(formatString))
.toEqual('testMonth testFormat')
expect(dayjs().format(formatString))
.toEqual(moment().format(formatString))
})
it('Update invalid date string', () => {
const locale = 'en'
const localeSetting = { invalidDate: 'bad date' }
dayjs.updateLocale(locale, localeSetting)
moment.updateLocale(locale, localeSetting)
dayjs.locale(locale)
moment.locale(locale)
expect(dayjs('').format()).toBe(moment('').format())
expect(dayjs('otherString').format()).toBe(moment('otherString').format())
})
})
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/test/plugin/isMoment.test.js | test/plugin/isMoment.test.js | import MockDate from 'mockdate'
import dayjs from '../../src'
import isMoment from '../../src/plugin/isMoment'
dayjs.extend(isMoment)
beforeEach(() => {
MockDate.set(new Date())
})
afterEach(() => {
MockDate.reset()
})
it('IsLeapYear', () => {
expect(dayjs.isMoment(dayjs())).toBe(true)
expect(dayjs.isMoment(new Date())).toBe(false)
})
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/test/plugin/duration.test.js | test/plugin/duration.test.js | import MockDate from 'mockdate'
import dayjs from '../../src'
import duration from '../../src/plugin/duration'
import relativeTime from '../../src/plugin/relativeTime'
import '../../src/locale/fr'
import '../../src/locale/es'
dayjs.extend(relativeTime)
dayjs.extend(duration)
beforeEach(() => {
MockDate.set(new Date())
})
afterEach(() => {
MockDate.reset()
})
describe('Creating', () => {
it('no argument', () => {
expect(dayjs.duration().toISOString()).toBe('P0D')
expect(dayjs.duration().asMilliseconds()).toBe(0)
})
it('milliseconds', () => {
expect(dayjs.duration(1, 'ms').toISOString()).toBe('PT0.001S')
expect(dayjs.duration(100).toISOString()).toBe('PT0.1S')
expect(dayjs.duration(1000).toISOString()).toBe('PT1S')
})
it('two argument will bubble up to the next', () => {
expect(dayjs.duration(59, 'seconds').toISOString()).toBe('PT59S')
expect(dayjs.duration(60, 'seconds').toISOString()).toBe('PT1M')
expect(dayjs.duration(13213, 'seconds').toISOString()).toBe('PT3H40M13S')
})
it('two argument will bubble up to the next (negative number)', () => {
expect(dayjs.duration(-59, 'seconds').toISOString()).toBe('-PT59S')
expect(dayjs.duration(-60, 'seconds').toISOString()).toBe('-PT1M')
expect(dayjs.duration(-13213, 'seconds').toISOString()).toBe('-PT3H40M13S')
})
it('object with float', () => {
expect(dayjs.duration({
seconds: 1,
minutes: 2,
hours: 3,
days: 4,
months: 6,
years: 7
}).toISOString()).toBe('P7Y6M4DT3H2M1S')
})
it('object with weeks and float', () => {
expect(dayjs.duration({
seconds: 1.1,
minutes: 2,
hours: 3,
days: 4,
weeks: 5,
months: 6,
years: 7
}).toISOString()).toBe('P7Y6M39DT3H2M1.1S')
})
it('object with millisecond', () => {
expect(dayjs.duration({
ms: 1
}).toISOString()).toBe('PT0.001S')
})
it('object with negative millisecond', () => {
expect(dayjs.duration({
ms: -1
}).toISOString()).toBe('-PT0.001S')
})
it('convert to milliseconds', () => {
expect(+dayjs.duration(100)).toBe(100)
})
it('handles rounding to millisecond precision', () => {
expect(dayjs.duration(2 / 3).toISOString()).toBe('PT0.001S')
})
it('should handle round with millisecond precision when negative', () => {
expect(dayjs.duration(1000.5).toISOString()).toBe('PT1.001S')
expect(dayjs.duration(-1000.5).toISOString()).toBe('-PT1S')
})
it('should handle floating point rounding errors', () => {
// An example of this is when adding 2 to 0.812 seconds, which is how
// the seconds component is calculated in .toISOString().
// > 2 + 0.812
// 2.8120000000000003
expect(dayjs.duration(-2812).toISOString()).toBe('-PT2.812S') // was -PT2.8120000000000003S
expect(dayjs.duration(3121632.27382247).toISOString()).toBe('PT52M1.632S') // was PT52M1.6320000000000001S
expect(dayjs.duration(7647826.525774224).toISOString()).toBe('PT2H7M27.827S') // was PT2H7M27.826999999999998S
})
})
describe('Parse ISO string', () => {
it('Full ISO string', () => {
expect(dayjs.duration('P7Y6M4DT3H2M1S').toISOString()).toBe('P7Y6M4DT3H2M1S')
})
it('Part ISO string', () => {
expect(dayjs.duration('PT2777H46M40S').toISOString()).toBe('PT2777H46M40S')
})
it('Formatting missing components', () => {
expect(dayjs.duration('PT1H').format('YYYY-MM-DDTHH:mm:ss')).toBe('0000-00-00T01:00:00')
})
it('ISO string with week', () => {
const d = dayjs.duration('P2M3W4D')
expect(d.toISOString()).toBe('P2M25D')
expect(d.asDays()).toBe(85.83333333333333) // moment 86, count 2M as 61 days
expect(d.asWeeks()).toBe(12.261904761904763) // moment 12.285714285714286
expect(d.asMonths()).toBe(2.8219178082191783) // moment 2.8213721020965523
})
it('Invalid ISO string', () => {
expect(dayjs.duration('Invalid').toISOString()).toBe('P0D')
})
})
it('Is duration', () => {
expect(dayjs.isDuration(dayjs.duration())).toBe(true)
expect(dayjs.isDuration(dayjs.duration(1))).toBe(true)
expect(dayjs.isDuration(dayjs())).toBe(false)
expect(dayjs.isDuration({})).toBe(false)
expect(dayjs.isDuration()).toBe(false)
})
it('toJSON', () => {
expect(JSON.stringify({
postDuration: dayjs.duration(5, 'minutes')
})).toBe('{"postDuration":"PT5M"}')
})
describe('Humanize', () => {
it('Humaniz', () => {
expect(dayjs.duration(1, 'minutes').humanize()).toBe('a minute')
expect(dayjs.duration(2, 'minutes').humanize()).toBe('2 minutes')
expect(dayjs.duration(24, 'hours').humanize()).toBe('a day')
expect(dayjs.duration(1, 'minutes').humanize(true)).toBe('in a minute')
expect(dayjs.duration(-1, 'minutes').humanize(true)).toBe('a minute ago')
})
it('Locale', () => {
expect(dayjs.duration(1, 'minutes').humanize(true)).toBe('in a minute')
expect(dayjs.duration(1, 'minutes').locale('fr').humanize(true)).toBe('dans une minute')
expect(dayjs.duration(1, 'minutes').locale('es').humanize(true)).toBe('en un minuto')
})
it('Global Locale', () => {
dayjs.locale('en')
expect(dayjs.duration(1, 'minutes').humanize(true)).toBe('in a minute')
dayjs.locale('fr')
expect(dayjs.duration(1, 'minutes').humanize(true)).toBe('dans une minute')
dayjs.locale('es')
expect(dayjs.duration(1, 'minutes').humanize(true)).toBe('en un minuto')
dayjs.locale('en')
})
})
describe('Clone', () => {
it('Locale clone', () => {
const d = dayjs.duration(1, 'minutes').locale('fr')
const r = 'dans une minute'
expect(d.humanize(true)).toBe(r)
expect(d.clone().humanize(true)).toBe(r)
})
})
describe('Milliseconds', () => {
expect(dayjs.duration(500).milliseconds()).toBe(500)
expect(dayjs.duration(1500).milliseconds()).toBe(500)
expect(dayjs.duration(15000).milliseconds()).toBe(0)
expect(dayjs.duration(500).asMilliseconds()).toBe(500)
expect(dayjs.duration(1500).asMilliseconds()).toBe(1500)
expect(dayjs.duration(15000).asMilliseconds()).toBe(15000)
})
describe('Milliseconds', () => {
describe('Positive number', () => {
expect(dayjs.duration(500).milliseconds()).toBe(500)
expect(dayjs.duration(1500).milliseconds()).toBe(500)
expect(dayjs.duration(15000).milliseconds()).toBe(0)
expect(dayjs.duration(500).asMilliseconds()).toBe(500)
expect(dayjs.duration(1500).asMilliseconds()).toBe(1500)
expect(dayjs.duration(15000).asMilliseconds()).toBe(15000)
})
describe('Negative number', () => {
expect(dayjs.duration(-500).milliseconds()).toBe(-500)
expect(dayjs.duration(-1500).milliseconds()).toBe(-500)
expect(dayjs.duration(-15000).milliseconds()).toBe(0)
expect(dayjs.duration(-500).asMilliseconds()).toBe(-500)
expect(dayjs.duration(-1500).asMilliseconds()).toBe(-1500)
expect(dayjs.duration(-15000).asMilliseconds()).toBe(-15000)
})
})
describe('Add', () => {
const a = dayjs.duration(1, 'days')
const b = dayjs.duration(2, 'days')
expect(a.add(b).days()).toBe(3)
expect(a.add(1, 'days').days()).toBe(2)
expect(a.add({ days: 5 }).days()).toBe(6)
})
describe('Add to a dayjs()', () => {
const a = dayjs()
const b = dayjs.duration({ hours: 7, minutes: 10 })
expect(a.add(b)).toEqual(a.add(7, 'hours').add(10, 'minutes'))
})
test('Add duration', () => {
const a = dayjs('2020-10-01')
const days = dayjs.duration(2, 'days')
expect(a.add(days).format('YYYY-MM-DD')).toBe('2020-10-03')
const b = dayjs('2023-02-01 00:00:00')
const p = dayjs.duration('P1Y1M1DT1H1M1S')
expect(b.add(p).format('YYYY-MM-DD HH:mm:ss')).toBe('2024-03-02 01:01:01')
})
describe('Subtract', () => {
const a = dayjs.duration(3, 'days')
const b = dayjs.duration(2, 'days')
expect(a.subtract(b).days()).toBe(1)
})
test('Subtract duration', () => {
const a = dayjs('2020-10-20')
const days = dayjs.duration(2, 'days')
expect(a.subtract(days).format('YYYY-MM-DD')).toBe('2020-10-18')
const b = dayjs('2023-03-02 02:02:02')
const p = dayjs.duration('P1Y1M1DT1H1M1S')
expect(b.subtract(p).format('YYYY-MM-DD HH:mm:ss')).toBe('2022-02-01 01:01:01')
})
describe('Seconds', () => {
expect(dayjs.duration(500).seconds()).toBe(0)
expect(dayjs.duration(1500).seconds()).toBe(1)
expect(dayjs.duration(15000).seconds()).toBe(15)
expect(dayjs.duration(61000).seconds()).toBe(1) // 1 minute 1 second
expect(dayjs.duration(500).asSeconds()).toBe(0.5)
expect(dayjs.duration(1500).asSeconds()).toBe(1.5)
expect(dayjs.duration(15000).asSeconds()).toBe(15)
})
describe('Minutes', () => {
expect(dayjs.duration(100000).minutes()).toBe(1)
expect(dayjs.duration(61000).minutes()).toBe(1) // 1 minute 1 second
expect(dayjs.duration(100000).asMinutes().toFixed(2)).toBe('1.67')
})
describe('Hours', () => {
expect(dayjs.duration(10000000).hours()).toBe(2)
expect(dayjs.duration(10000000).asHours().toFixed(2)).toBe('2.78')
})
describe('Days', () => {
it('positive number', () => {
expect(dayjs.duration(100000000).days()).toBe(1)
expect(dayjs.duration(100000000).asDays().toFixed(2)).toBe('1.16')
})
it('negative number', () => {
expect(dayjs.duration(-1).days()).toBe(0)
expect(dayjs.duration(-86399999).asDays()).toBeCloseTo(-0.999999, 4)
})
})
describe('Weeks', () => {
expect(dayjs.duration(1000000000).weeks()).toBe(1)
expect(dayjs.duration(1000000000).asWeeks().toFixed(2)).toBe('1.65')
})
describe('Month', () => {
expect(dayjs.duration(10000000000).months()).toBe(3)
expect(dayjs.duration({ months: 3 }).asMonths()).toBe(3)
})
describe('Years', () => {
expect(dayjs.duration(100000000000).years()).toBe(3)
expect(dayjs.duration(100000000000).asYears().toFixed(2)).toBe('3.17')
})
describe('prettyUnit', () => {
const d = dayjs.duration(2, 's')
expect(d.toISOString()).toBe('PT2S')
expect(d.as('seconds')).toBe(2)
expect(d.get('s')).toBe(2)
expect(dayjs.duration({
M: 12,
m: 12
}).toISOString()).toBe('P12MT12M')
})
describe('Format', () => {
test('no formatStr', () => {
const d = dayjs.duration(15, 'seconds')
.add(13, 'hours')
.add(35, 'minutes')
.add(16, 'days')
.add(10, 'months')
.add(22, 'years')
expect(d.format()).toBe('0022-10-16T13:35:15')
})
test('with formatStr for all tokens', () => {
const d = dayjs.duration(1, 'seconds')
.add(8, 'hours')
.add(5, 'minutes')
.add(6, 'days')
.add(9, 'months')
.add(2, 'years')
.add(10, 'milliseconds')
expect(d.format('Y/YY.YYYYTESTM:MM:D:DD:H:HH:m:mm:s:ss:SSS'))
.toBe('2/02.0002TEST9:09:6:06:8:08:5:05:1:01:010')
})
})
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/test/plugin/isToday.test.js | test/plugin/isToday.test.js | import MockDate from 'mockdate'
import dayjs from '../../src'
import isToday from '../../src/plugin/isToday'
dayjs.extend(isToday)
beforeEach(() => {
MockDate.set(new Date())
})
afterEach(() => {
MockDate.reset()
})
it('is today', () => {
expect(dayjs(new Date()).isToday()).toBeTruthy()
expect(dayjs('2017-01-01').isToday()).toBeFalsy()
})
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/test/plugin/arraySupport.test.js | test/plugin/arraySupport.test.js | import MockDate from 'mockdate'
import moment from 'moment'
import dayjs from '../../src'
import arraySupport from '../../src/plugin/arraySupport'
import utc from '../../src/plugin/utc'
dayjs.extend(utc)
dayjs.extend(arraySupport)
beforeEach(() => {
MockDate.set(new Date())
})
afterEach(() => {
MockDate.reset()
})
describe('parse empty array', () => {
it('local', () => {
expect(dayjs([]).format())
.toBe(moment([]).format())
})
it('utc', () => {
expect(dayjs.utc([]).format())
.toBe(moment.utc([]).format())
})
})
const testArrs = [
[2010, 1, 14, 15, 25, 50, 125],
[2010],
[2010, 6],
[2010, 6, 10]
]
describe('parse array local', () => {
testArrs.forEach((testArr) => {
it(testArr, () => {
expect(dayjs(testArr).format())
.toBe(moment(testArr).format())
})
})
})
describe('parse array utc', () => {
testArrs.forEach((testArr) => {
it(testArr, () => {
expect(dayjs.utc(testArr).format())
.toBe(moment.utc(testArr).format())
})
})
})
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
iamkun/dayjs | https://github.com/iamkun/dayjs/blob/54f447048cee679e51a7053f8042d9b6b7028b89/test/plugin/quarterOfYear.test.js | test/plugin/quarterOfYear.test.js | import MockDate from 'mockdate'
import moment from 'moment'
import dayjs from '../../src'
import quarterOfYear from '../../src/plugin/quarterOfYear'
dayjs.extend(quarterOfYear)
beforeEach(() => {
MockDate.set(new Date())
})
afterEach(() => {
MockDate.reset()
})
it('get QuarterOfYear', () => {
expect(dayjs('2013-01-01T00:00:00.000').quarter()).toBe(1)
expect(dayjs('2013-04-01T00:00:00.000').subtract(1, 'ms').quarter()).toBe(1)
expect(dayjs('2013-04-01T00:00:00.000').quarter()).toBe(2)
expect(dayjs('2013-07-01T00:00:00.000').subtract(1, 'ms').quarter()).toBe(2)
expect(dayjs('2013-07-01T00:00:00.000').quarter()).toBe(3)
expect(dayjs('2013-10-01T00:00:00.000').subtract(1, 'ms').quarter()).toBe(3)
expect(dayjs('2013-10-01T00:00:00.000').quarter()).toBe(4)
expect(dayjs('2014-01-01T00:00:00.000').subtract(1, 'ms').quarter()).toBe(4)
})
it('set QuarterOfYear', () => {
const d1 = '2013-01-01T00:00:00.000'
expect(dayjs(d1).quarter(2).format())
.toBe(moment(d1).quarter(2).format())
const d2 = '2013-02-05T05:06:07.000'
expect(dayjs(d2).quarter(2).format())
.toBe(moment(d2).quarter(2).format())
const d3 = '2018-11-25T05:06:07.000'
expect(dayjs(d3).quarter(3).format())
.toBe(moment(d3).quarter(3).format())
})
it('add subtract quarter', () => {
expect(dayjs().add(2, 'quarter').format())
.toBe(moment().add(2, 'quarter').format())
expect(dayjs().subtract(2, 'quarter').format())
.toBe(moment().subtract(2, 'quarter').format())
})
it('startOf endOf quarter', () => {
expect(dayjs().startOf('quarter').format())
.toBe(moment().startOf('quarter').format())
expect(dayjs().endOf('quarter').format())
.toBe(moment().endOf('quarter').format())
})
| javascript | MIT | 54f447048cee679e51a7053f8042d9b6b7028b89 | 2026-01-04T14:57:31.496629Z | false |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.