创建
要创建持续时间,请使用以毫秒为单位的时间长度调用 dayjs.duration()
。
¥To create a duration, call dayjs.duration()
with the length of time in milliseconds.
This requires the
Duration
plugin to work
dayjs.extend(duration)
dayjs.duration(100); // 100 milliseconds
如果你想使用毫秒以外的测量单位创建持续时间,你也可以传递测量单位。
¥If you want to create a duration with a unit of measurement other than milliseconds, you can pass the unit of measurement as well.
dayjs.duration(2, 'days');
所有可用单位的列表
¥List of all available units
单位 | 速记 |
---|---|
days | d |
weeks | w |
months | M |
years | y |
hours | h |
minutes | m |
seconds | s |
milliseconds | ms |
如果需要多个不同的测量单位,你还可以传递值对象。
¥You can also pass an object of values if you need multiple different units of measurement.
dayjs.duration({
seconds: 2,
minutes: 2,
hours: 2,
days: 2,
weeks: 2,
months: 2,
years: 2
});
Day.js 还支持解析 ISO 8601 持续时间。
¥Day.js also supports parsing ISO 8601 durations.
dayjs.duration('P1Y2M3DT4H5M6S');
dayjs.duration('P1M');