我想使用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/

10-12 03:33