日曆時間
日曆時間以更人性化的方式,顯示相對於參考時間(預設為當前時間)的時間,與 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 )
});