vue日期转换

37 阅读1分钟

1.周几转换成对应日期

function getWeekDay(day) {
  if (day < 1) {
    day = 1
  }
  if (day > 7) {
    day = 7
  }
  var currDate = new Date()
  currDate = new Date(
    currDate.getFullYear(),
    currDate.getMonth(),
    currDate.getDate()
  ) // 取精确到天,去掉时分秒
  var currDay = currDate.getDay()
  if (currDay == 0) {
    currDay = 7
  }
  let timeRes
  if (currDay == day) {
    timeRes = currDate
  }
  var dayMs = 1000 * 60 * 60 * 24
  var diffMs = Math.abs(currDay - day) * dayMs

  if (currDay > day) {
    timeRes = new Date(currDate.getTime() - diffMs)
  } else {
    timeRes = new Date(currDate.getTime() + diffMs)
  }
  let date = new Date(timeRes)
  let year = date.getFullYear()
  let month = Number(date.getMonth() + 1)
  let da = Number(date.getDate())
  return year + '-' + month + '-' + da
}

2.2023-06-01T08:34:18.000+0000转换成2023-06-01

   formatDate(time) {
      let date = new Date(time)
      let year = date.getFullYear()
      let month = this.format(date.getMonth() + 1)
      let da = this.format(date.getDate())
      let h = this.format(date.getHours())
      let m = this.format(date.getMinutes())
      let s = this.format(date.getSeconds())
      return year + '-' + month + '-' + da + ' ' + h + ':' + m + ':' + s
    },
    format(val) {
      return Number(val) < 10 ? '0' + val : '' + val
    },