时长
Day.js 也有持续时间对象。其中 Day.js 对象被定义为单个时间点,持续时间被定义为时间长度。
¥Day.js also has duration objects. Where a Day.js object is defined as single points in time, durations are defined as a length of time.
持续时间没有定义的开始和结束日期。它们是无上下文的。
¥Durations do not have a defined beginning and end date. They are contextless.
持续时间在概念上更类似于 '2 hours' 而不是 'between 2 and 4 pm today'。因此,它们不是在依赖上下文的单位之间进行转换的好解决方案。
¥A duration is conceptually more similar to '2 hours' than to 'between 2 and 4 pm today'. As such, they are not a good solution to converting between units that depend on context.
例如,一年可以定义为 366 天、365 天、365.25 天、12 个月或 52 周。没有上下文,试图将年转换为天是没有意义的。使用 dayjs#diff
计算两个日期时间之间的天数或年数比使用持续时间要好得多。
¥For example, a year can be defined as 366 days, 365 days, 365.25 days, 12 months, or 52 weeks. Trying to convert years to days makes no sense without context. It is much better to use dayjs#diff
for calculating days or years between two date times than to use Durations.
This requires the
Duration
plugin to work
dayjs.extend(duration)
dayjs.duration({ months: 12 })