vue2格式化时间戳

注意:时间戳分为10位和13位的,10位的是秒,13位的是毫秒

这里给出的是格式化13位的方法,10位的时间戳可以加上3个0

<div id="app">{{time | dataFormat('yyyy-MM-dd hh:mm:ss')}}</div>
Vue.filter('dataFormat', function (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;
});
    var test=new Vue({
        el:"#app",
        data:{
            
            time:1558961387000
        }
        
    });

猜你喜欢

转载自www.cnblogs.com/jcydd/p/10933128.html
今日推荐