gpt4 book ai didi

javascript - 如何获得现在和下周五之间的时差

转载 作者:行者123 更新时间:2023-11-30 11:04:13 25 4
gpt4 key购买 nike

我想使用 momentjs 获得现在和下周五 18:00(周末)之间的时差,我试过这个但它不起作用:

public getWeekend() {
const nextFriday = moment().weekday(4)
nextFriday.hours(18) // Weekend time: 18:00
nextFriday.minutes(0)
nextFriday.seconds(0)

const diff = moment(nextFriday.diff(moment()))
console.log(diff.days())
if (diff.days() * 24 + diff.hours() <= 99) {
return nextFriday.toString()
} else {
return 0
}
}
}

console.log(diff.days()) 正在打印 6 天,而我们是星期三

最佳答案

试试这个:

function getWeekend() {

const nextFriday = moment().weekday(4)
nextFriday.hours(18) // Weekend time: 18:00
nextFriday.minutes(0)
nextFriday.seconds(0)

const diffHours = moment.duration(nextFriday.diff(moment())).asHours()
console.log(`${diffHours} hours`) // 31.7 hours
if (diffHours <= 99) {
return nextFriday.toString()
} else {
return 0
}
}

关于javascript - 如何获得现在和下周五之间的时差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56356669/

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