vue中格式化时间戳(可直接使用)

<template>
<div>
<div id="app">{{1596073020000 | dataFormat('yyyy-MM-dd hh:mm:ss')}}</div>
</div>
</template>
<script >
 
  export default {
    data() {
    },
    filters: {       //格式化时间戳的过滤器(注意:时间戳是数字格式,不是字符串格式,不然会显示为Nan)
  dataFormat(value, fmt) {
      let getDate = new Date(value);
      let o = {
        'M+': getDate.getMonth() + 1,
        'd+': getDate.getDate(),
        'h+': getDate.getHours(),
        'm+': getDate.getMinutes(),
        's+': getDate.getSeconds(),
        'q+': Math.floor((getDate.getMonth() + 3) / 3),
        'S': getDate.getMilliseconds()
      };
      if (/(y+)/.test(fmt)) {
          fmt = fmt.replace(RegExp.$1, (getDate.getFullYear() + '').substr(4 - RegExp.$1.length))
      }
      for (let k in o) {
          if (new RegExp('(' + k + ')').test(fmt)) {
              fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
          }
      }
      return fmt;
    },
    },  
  }
</script>
原文地址:https://www.cnblogs.com/ilylmy/p/13587028.html