js计算两个日期相差天数

先将两个日期都转为时间戳,相减后绝对值除以(2436001000)就是天数

getDays (start, end) { // 2019-01-21,2019-01-23
  let dateSpan, iDays
  start = Date.parse(start)
  end = Date.parse(end)
  dateSpan = end - start
  dateSpan = Math.abs(dateSpan)
  iDays = Math.floor(dateSpan / (24 * 3600 * 1000))
  return iDays;
}

获取当前日期:

var timestamp = new Date().getTime() // 当前时间戳
getDate (timestamp) {
  let date = new Date(timestamp)
  let year = date.getFullYear()
  let month = date.getMonth() + 1
  let day = date.getDate()
  let week = '周' + '日一二三四五六'.charAt(date.getDay())
  if (month < 10) {
    month = '0' + month
  }
  if (day < 10) {
    day = '0' + day
  }
  return year + '-' + month + '-' + day; // 2019-01-21
}

你可能感兴趣的:(js计算两个日期相差天数)