我的逻辑有点问题,我希望能够自动计算两个日期之间的天数或月数的差异,例如:还有 20 天,还有 1 个月和 17 天,...函数直接用 dayjs 做到这一点,我似乎无法得到我想要的逻辑。
预先感谢您的帮助 :)
暂时……
let creation = dayjs(currentEstimate.createdAt)
let limit_date = dayjs(currentEstimate.createdAt).add(currentEstimate.estimate_validation_date, 'd')
let diff = dayjs(limit_date).diff(creation, 'd')
let months = diff / 31
if (months < 1){
console.log(`devis valable ${diff} jours`)
}else{
console.log(`devis valable ${months} mois`)
}
编辑
感谢斯蒂芬·柯林斯->
let duration = require('dayjs/plugin/duration')
dayjs.extend(duration)
let relativeTime = require('dayjs/plugin/relativeTime')
dayjs.extend(relativeTime)
dayjs(currentEstimate.createdAt).to(dayjs(currentEstimate.createdAt).add(currentEstimate.estimate_validation_date, 'd'), true)
//one month