Day.js 中文文档

当前版本 v1.11

更多文档

时间差

这表示指定单位中两个日期时间之间的差异。

要获得以毫秒为单位的差异,请使用 dayjs#diff

const date1 = dayjs('2019-01-25')
const date2 = dayjs('2018-06-05')
date1.diff(date2) // 20214000000 default milliseconds

要获得另一个测量单位的差异,请将该测量值作为第二个参数传递。

const date1 = dayjs('2019-01-25')
date1.diff('2018-06-05', 'month') // 7

默认情况下,dayjs#diff 会将结果截断为零位小数,返回一个整数。如果你想要浮点数,请传递 true 作为第三个参数。

const date1 = dayjs('2019-01-25')
date1.diff('2018-06-05', 'month', true) // 7.645161290322581

所有可用单位的列表

单位不区分大小写,并支持复数和缩写形式。请注意,缩写形式区分大小写。

单位速记描述
dayd
weekw
quarterQ季度
monthM月份(1 月为 0,12 月为 11)
yeary年份
hourh小时
minutem分钟
seconds
millisecondms毫秒

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