日期格式轉換,轉換格式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>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM