Day.js 中文文档

当前版本 v1.11

更多文档

日历时间

日历时间显示相对于给定参考时间(默认为现在)的时间,但与 dayjs#fromNow 略有不同。

需要配合 Calendar 插件才能工作

dayjs.extend(calendar)

dayjs().calendar()
dayjs().calendar(dayjs('2008-01-01'))
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])。

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 )
})

Day.js 中文文档 - 粤ICP备14034220号-1