(1)創(chuàng)建一個(gè)處理時(shí)間格式的js庆揪,內(nèi)容如下:
Date.prototype.Format = function(fmt) { //
let o = {
"M+" : this.getMonth()+1, //月份
"d+" : this.getDate(), //日
"h+" : this.getHours(), //小時(shí)
"m+" : this.getMinutes(), //分
"s+" : this.getSeconds(), //秒
"q+" : Math.floor((this.getMonth()+3)/3), //季度
"S" : this.getMilliseconds() //毫秒
};
if(/(y+)/.test(fmt))
fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
for(var 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;
};
(2)在vue文件中需要格式化時(shí)間戳的地方戒幔,使用filters過(guò)濾器幌陕,做如下處理:
<template>
<div class="date">{{item.pass_time | Format }}</div>
</template>
<script type="text/ecmascript-6">
import {Format } from '../../utils/Format .js''
export default {
filters: {
//注意:time應(yīng)為格式為13位unix時(shí)間戳
//年-月-日 時(shí):分:秒
Format (time) {
time = time * 1000
let date = new Date(time)
console.log(new Date(time))
return Format (date, 'yyyy-MM-dd hh:mm')
}
//年-月-日
Format (time) {
time = time * 1000
let date = new Date(time)
console.log(new Date(time))
return Format (date, 'yyyy-MM-dd hh:mm')
}
}
}
</script>