dayjs 的功能相对于 moment 有缺失, 有些东西需要自己去写.
github issues:
https://github.com/iamkun/dayjs/issues/2133
版本号:
"dayjs": "^1.11.6"
配置:
import dayjs from 'dayjs';
import 'dayjs/locale/zh-cn';
import calenderPlugin from 'dayjs/plugin/calendar';
import updateLocale from 'dayjs/plugin/updateLocale';
// 注册插件
dayjs.extend(updateLocale);
dayjs.extend(calenderPlugin);
// 修改语言配置
dayjs.updateLocale('zh-cn', {
// A : 上午/下午/晚上 , dddd: 星期
calendar: {
lastDay: 'YYYY.MM.DD [昨天] A h:mm dddd',
sameDay: 'YYYY.MM.DD [今天] A h:mm dddd',
nextDay: 'YYYY.MM.DD [明天] A h:mm dddd',
lastWeek: 'YYYY.MM.DD A h:mm [上]dddd',
nextWeek: 'YYYY.MM.DD A h:mm [下]dddd',
sameElse: 'YYYY.MM.DD A h:mm dddd',
},
});
// 使用 zh-cn
dayjs.locale('zh-cn');
测试:
// code:
<div>{dayjs("2022-11-25 10:22:34").calendar()}</div>
<div>{dayjs("2022-11-26 16:22:34").calendar()}</div>
<div>{dayjs("2022-11-27 20:22:34").calendar()}</div>
<div>{dayjs("2022-11-20 16:22:34").calendar()}</div>
<div>{dayjs("2022-11-30 16:22:34").calendar()}</div>
<div>{dayjs("2021-11-26 16:22:34").calendar()}</div>
// output:
2022.11.25 昨天 上午 10:22 星期五
2022.11.26 今天 下午 4:22 星期六
2022.11.27 明天 晚上 8:22 星期日
2022.11.20 下午 4:22 上星期日
2022.11.30 下午 4:22 下星期三
2021.11.26 下午 4:22 星期五