day.js diff 方法详解
在 day.js 中 diff 方法返回指定单位下两个日期时间之间的差异,默认以毫秒为单位,返回值可以为负数。示例一、基础使用默认返回毫秒差异const date1 = dayjs('2022-11-14') const date2 = dayjs('2022-01-25') date1.diff(date2) // 25315200000 date2.diff(date1) // -25315200000示例二、指定差异单位要获取其他单位下的差异,则在第二个参数传入相应的单位。const date1 = dayjs('2022-11-14') const date2 = dayjs('2022-01-25') date1.diff(date2, 'month') // 9示例三、获得更高精度的差异默认情况下 dayjs#diff 会将结果进位成整数。 如果要得到一个浮点数,将 true 作为第三个参数传入。const date1 = dayjs('2022-11-14') const date2 = dayjs('2022-01-25') date1.diff(date2, 'week',true) //41.857142857142854支持的单位列表传入的单位对大小写不敏感,同时支持缩写和复数。 但是缩写是区分大小写的。单位缩写详情dayd星期几 (星期天0,星期六6)weekw周quarterQ季度monthM月份 (一月 0, 十二月 11)yeary年hourh时minutem分seconds秒millisecondms毫秒
251 阅读
0 评论