Vue 获取时间戳返回自定义时间格式

直接在Vue全局函数定义:

    Vue.prototype.padLeftZero = function(str) {
        return ('00' + str).substr(str.length);
    };
    Vue.prototype.formatDate = function(date, fmt) {
        var o = {
            'M+': date.getMonth() + 1, //月份
            'd+': date.getDate(), //
            'h+': date.getHours(), //小时
            'm+': date.getMinutes(), //
            's+': date.getSeconds(), //
            "q+": Math.floor((date.getMonth() + 3) / 3), //季度   
            "S": date.getMilliseconds() //毫秒   

        };
        if (/(y+)/.test(fmt)) { //年份
              
            fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
        }
        for (let k in o) {
            if (new RegExp("(" + k + ")").test(fmt)) {
                var str = o[k] + '';
                fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : this.padLeftZero(str));  
            }
        }
        return fmt;
    };

 

  调用方法:

  this.formatDate(new Date(Time*1000),'yyyy-MM-dd hh:mm:ss')
  第二个参数根据实际需要修改
posted @ 2019-05-09 16:07  bomdeyada  阅读(490)  评论(0编辑  收藏  举报