RelativeTime
RelativeTime 插件添加了 .from
、.to
、.fromNow
和 .toNow
API,用于将日期格式化为相对时间字符串(例如,“3 小时前”)。
javascript
var relativeTime = require('dayjs/plugin/relativeTime');
// import relativeTime from 'dayjs/plugin/relativeTime' // ES 2015
dayjs.extend(relativeTime);
dayjs().from(dayjs('1990-01-01')); // 31 年前
dayjs().from(dayjs('1990-01-01'), true); // 31 年
dayjs().fromNow();
dayjs().to(dayjs('1990-01-01')); // "31 年前"
dayjs().toNow();
距现在的时间 .fromNow(withoutSuffix?: boolean)
返回从当前时间开始计算的相对时间字符串。
距 X 的时间 .from(compared: Dayjs, withoutSuffix?: boolean)
返回从指定时间 X 开始计算的相对时间字符串。
到现在的时间 .toNow(withoutSuffix?: boolean)
返回到当前时间为止的相对时间字符串。
到 X 的时间 .to(compared: Dayjs, withoutSuffix?: boolean)
返回到指定时间 X 为止的相对时间字符串。