人性化
有时,你想要 dayjs#from
的所有优点,但又不想创建两个 Day.js 对象,你只想显示一段时间。
¥Sometimes, you want all the goodness of dayjs#from
but you don't want to have to create two Day.js objects, you just want to display a length of time.
This requires the
Duration
plugin to work
This requires the
RelativeTime
plugin to work
dayjs.extend(duration)
dayjs.extend(relativeTime)
dayjs.duration(1, "minutes").humanize(); // a minute
dayjs.duration(2, "minutes").humanize(); // 2 minutes
dayjs.duration(24, "hours").humanize(); // a day
默认情况下,返回字符串是无后缀的。如果你想要后缀,请传入 true,如下所示。
¥By default, the return string is suffixless. If you want a suffix, pass in true as seen below.
dayjs.duration(1, "minutes").humanize(true); // in a minute
对于现在之前的后缀,传入一个负数。
¥For suffixes before now, pass in a negative number.
dayjs.duration(-1, "minutes").humanize(true); // a minute ago