时间业务
参考文章:业务上会用到的时间处理(持续更新) (opens new window)
# 时间增加、减少
时间增加
dayjs().add(7, 'day')
1
时间减少
dayjs().subtract(7, 'day')
1
# 获取时间差
const date1 = dayjs('2019-01-25')
date1.diff('2018-06-05', 'month', true) // 7.645161290322581 如果要得到一个浮点数,将 true 作为第三个参数传入
1
2
2
# 格式化时间
dayjs('2019-01-25').format('DD/MM/YYYY')
1
支持的格式化占位符列表 (opens new window)
# 判断是否在两个日期之间
const currentTime = dayjs()
currentTime.isAfter(dayjs('2011-01-01')) &&
currentTime.isBefore(dayjs('2033-01-01'))
1
2
3
2
3
或者
dayjs.extend(isBetween)
dayjs('2010-10-20').isBetween('2010-10-19', dayjs('2010-10-25'))
// 也可以:
// dayjs().isBetween('2010-10-19', '2010-10-25', 'month')
1
2
3
4
2
3
4
上次更新: 2023/12/16, 09:22:46