diff --git a/src/locale/zh.js b/src/locale/zh.js new file mode 100644 index 000000000..b412dc8c2 --- /dev/null +++ b/src/locale/zh.js @@ -0,0 +1,71 @@ +// Chinese [zh] +import dayjs from 'dayjs' + +const locale = { + name: 'zh', + weekdays: '星期日_星期一_星期二_星期三_星期四_星期五_星期六'.split('_'), + weekdaysShort: '周日_周一_周二_周三_周四_周五_周六'.split('_'), + weekdaysMin: '日_一_二_三_四_五_六'.split('_'), + // prettier conflicts with eslint, so + // prettier-ignore + 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) => { + // prettier conflicts with eslint, so + // prettier-ignore + const hm = (hour * 100) + minute + if (hm < 600) { + return '凌晨' + } else if (hm < 900) { + return '早上' + } else if (hm < 1130) { + return '上午' + } else if (hm < 1230) { + return '中午' + } else if (hm < 1800) { + return '下午' + } + return '晚上' + } +} + +dayjs.locale(locale, null, true) + +export default locale diff --git a/test/locale/zh.test.js b/test/locale/zh.test.js new file mode 100644 index 000000000..2748a510a --- /dev/null +++ b/test/locale/zh.test.js @@ -0,0 +1,20 @@ +import dayjs from '../../src' +import advancedFormat from '../../src/plugin/advancedFormat' +import weekOfYear from '../../src/plugin/weekOfYear' +import '../../src/locale/zh.js' +import '../../src/locale/zh-cn.js' + +dayjs.extend(advancedFormat).extend(weekOfYear) + +const zh = dayjs().locale('zh') +const zhCN = dayjs().locale('zh-cn') + +test('ordinal', () => { + expect(zh.format('wo')).toEqual(`第${zh.format('w')}周`) +}) + +test('Meridiem', () => { + for (let i = 0; i <= 24; i += 1) { + expect(zh.add(i, 'hour').format('A')).toBe(zhCN.add(i, 'hour').format('A')) + } +})