日期格式转换,转换格式YYYY-MM-DD HH:mm:ss

// 获取当前时间,格式YYYY-MM-DD HH:mm:ss
export function formatDate (timeStamp) {
  // 解决IE转换日期格式不支持-,需要替换为/或者再转为时间戳
  let isIe = ('' + timeStamp).indexOf('-')
  if (isIe > 1) {
    timeStamp = Date.parse(timeStamp.replace(/-/g, '/'))
  }
  let date = new Date(timeStamp)
  let seperator1 = '-'
  let seperator2 = ':'
  let year = date.getFullYear()
  let month = date.getMonth() + 1
  let strDate = date.getDate()
  let hour = date.getHours()
  let minute = date.getMinutes()
  let seconds = date.getSeconds()
  if (month >= 1 && month <= 9) {
    month = '0' + month
  }
  if (strDate >= 0 && strDate <= 9) {
    strDate = '0' + strDate
  }
  if (hour >= 0 && hour <= 9) {
    hour = '0' + hour
  }
  if (minute >= 0 && minute <= 9) {
    minute = '0' + minute
  }
  if (seconds >= 0 && seconds <= 9) {
    seconds = '0' + seconds
  }
  let currentdate = year + seperator1 + month + seperator1 + strDate + ' ' + hour + seperator2 + minute + seperator2 + seconds
  return currentdate
}

全局过滤器

Vue.filter('FormateTimeNoSeconds', function (value) {
  return value ? formatDateNoSeconds(value) : ''
})

组件内使用

<span v-else>{{ time | FormateTimeNoSeconds }}</span>
原文地址:https://www.cnblogs.com/adbg/p/13689891.html