dateUtil.js

/**
 * 格式化日期
 * @param {*} timestamp 要转换的时间戳
 * @param {*} formate 格式的格式  支持yyyy-MM-dd HH:mm:ss和yyyy-MM-dd两种
 * @param {*} separator 分隔符
 */
const timestampToTime = (timestamp, formate, separator) => {
  let result = timestamp // 字符串的话原封不动返回
  if (timestamp && typeof timestamp !== ''string'') {
    if (timestamp.toString().length === 10) {
      timestamp = timestamp * 1000 /// /时间戳为10位需*1000,时间戳为13位的话不需乘1000
    }
    separator = separator || ''-''
    let date = new Date(timestamp)
    let year = date.getFullYear() + separator
    let month =
      (date.getMonth() + 1 < 10
        ? ''0'' + (date.getMonth() + 1)
        : date.getMonth() + 1) + separator
    // let nowDate = date.getDate() + '' '';
    let nowDate = date.getDate() < 10 ? ''0'' + date.getDate() : date.getDate()
    let hour = date.getHours()
    hour = hour < 10 ? ''0'' + hour : hour
    hour = hour + '':''
    let minutes = date.getMinutes()
    minutes = minutes < 10 ? ''0'' + minutes : minutes
    minutes = minutes + '':''
    let second = date.getSeconds()
    second = second < 10 ? ''0'' + second : second
    if (formate === ''yyyy-MM-dd'') {
      result = result = year + month + nowDate
      return result
    }
    nowDate += '' ''
    result = year + month + nowDate + hour + minutes + second
  }
  return result
}

计算目前到目标日期还剩多少天

引入
import dateUtil from '@/util/dateUtil'

validEndDate (endTime) {
   //'目标时间:‘2020-5-4’' 后面跟上23:59:59   得到最后的毫秒数
      const endTimeStr = `${endTime} 23:59:59`
      const defaultFormat = ''yyyy-MM-dd''
      const timed = new Date().getTime()
      const startTimeStamp = dateUtil.timestampToTime(timed, defaultFormat)//得到当前‘2020-5-1’
      const startTimeStr = `${startTimeStamp} 00:00:00`//后面跟上00:00:00  
      const startTime = new Date(startTimeStr).getTime()//从0点开始的毫秒数
      const endTimeCompu = new Date(endTimeStr).getTime() //得到最后的毫秒数
      const timeDiff = Math.abs(endTimeCompu - startTime)//计算相差的毫秒数
      const day = timeDiff / (24 * 60 * 60 * 1000)//换算成天数
      this.toDate = Math.ceil(day)
    },

HappyCodingTop
526 声望847 粉丝

Talk is cheap, show the code!!