时间戳转化成时间格式,以前也写过,但是使用vue的filter配合使用还是头一次,话不多说,请看代码:
js:
1 var app = new Vue({ 2 el: '#main-info', 3 filters: { 4 formatDatetwo: function (time) { 5 var re = /-?\d+/; 6 var m = re.exec(time); 7 var d = new Date(parseInt(m[0])); 8 var o = { 9 "M+": d.getMonth() + 1, //month 10 "d+": d.getDate(), //day 11 "h+": d.getHours(), //hour 12 "m+": d.getMinutes(), //minute 13 "s+": d.getSeconds(), //second 14 "q+": Math.floor((d.getMonth() + 3) / 3), //quarter 15 "S": d.getMilliseconds() //millisecond 16 } 17 var format = "yyyy-MM-dd"; 18 if (/(y+)/.test(format)) { 19 format = format.replace(RegExp.$1,(d.getFullYear() + "").substr(4 - RegExp.$1.length)); 20 } 21 for (var k in o) { 22 if (new RegExp("(" + k + ")").test(format)) { 23 format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length)); 24 } 25 } 26 return format; 27 } 28 } 29 })
页面中调用:
<p >{{time|formatDatetwo}}</p>