gpt4 book ai didi

javascript - 时刻没有给我两个日期之间的天数差异

转载 作者:行者123 更新时间:2023-11-28 12:53:44 25 4
gpt4 key购买 nike

我有一个这样的代码,其中有我的日期和当前/今天的日期,我需要获得两个日期之间的天数差异。我总是得到 0 或某个大数字“31536000000”出了什么问题?

const startDate = moment(job.published_date, 'YYYY-MM-DD').format('YYYY,MM,DD')  //looks like this "2018,08,19"
const endDate = moment(new Date()).format('YYYY,MM,DD') //looks like this "2019,09,12"
const a = moment([ startDate, ])
const b = moment([ endDate, ])
console.log('DIFFERENCE IN DAYS', b.diff(a))

最佳答案

const startDate = moment('2017-08-16', 'YYYY-MM-DD').format('YYYY,MM,DD')  //looks like this "2018,08,19"
const endDate = moment(new Date()).format('YYYY,MM,DD') //looks like this "2019,09,12"
const a = moment([ startDate, ])
const b = moment([ endDate, ])
console.log('DIFFERENCE IN DAYS', moment.duration(b.diff(a)).asDays());
// DIFFERENCE IN DAYS 730

您需要从差异发生后的那一刻起获取持续时间值 - 然后将其格式化为天。

这有帮助

Get hours difference between two dates in Moment Js

关于javascript - 时刻没有给我两个日期之间的天数差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57911326/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com