日历时间
日历时间以更人性化的方式,显示相对于参考时间(默认为当前时间)的时间,与 dayjs#fromNow
的功能类似,但展示方式略有不同。
TIP
此功能需要 Calendar 插件才能使用。
js
dayjs.extend(calendar);
dayjs().calendar();
dayjs().calendar(dayjs('2008-01-01'));
Key | Value |
---|---|
Last week (lastWeek) | Last Monday at 2:30 AM |
The day before (lastDay) | Yesterday at 2:30 AM |
The same day (sameDay) | Today at 2:30 AM |
The next day (nextDay) | Tomorrow at 2:30 AM |
The next week (nextWeek) | Sunday at 2:30 AM |
Everything else (sameElse) | 7/10/2011 |
这些字符串是本地化的,并且可以自定义。
您还可以通过第二个参数指定日历输出格式。
要转义字符,请将其用方括号括起来(例如 [Today]
)。
js
dayjs().calendar(null, {
sameDay: '[Today at] h:mm A', // The same day ( Today at 2:30 AM )
nextDay: '[Tomorrow]', // The next day ( Tomorrow at 2:30 AM )
nextWeek: 'dddd', // The next week ( Sunday at 2:30 AM )
lastDay: '[Yesterday]', // The day before ( Yesterday at 2:30 AM )
lastWeek: '[Last] dddd', // Last week ( Last Monday at 2:30 AM )
sameElse: 'DD/MM/YYYY', // Everything else ( 7/10/2011 )
});