接到后台给返回数据的时间戳是酱紫的:1525251272000
前端要把它转换成正常的时间格式,需要添加过滤器,要怎么做呢?
vue-cli方法
html:
<div class="inbox-list" v-for="(message, index) in messages" :key="index">
<div class="list-data">{{message.create_time|formatDate}}</div>
</div>
js:
import Utils from "../../Utils/Utils.js";
import Url from "../UrlApi/JavaUrlApiModify.js";
export default {
data() {
return {
messages: []
};
},
methods: {
inboxShow() {
let self = this;
self
.$fetch(self.basePathEmail + Url.inboxReadMessUrl())
.then(result => {
self.messages = result.data;
})
.catch(error => {
console.log(error);
});
},
},
mounted() {
this.inboxShow();
},
filters: {
formatDate: function(time) {
var myDate = new Date(time);
var year = myDate.getFullYear();
var month = myDate.getMonth() + 1;
var date = myDate.getDate();
return (year + '-' + month + '-' + date);
}
}
};
Date有个坑就是日子是从1开始计算的,月份是从0开始算的,所以在获取月份的时候要+1;
new Date中要写参数,否则始终获取的是当天的日期。
jquery方法
var myDate = new Date(createTime);
var year = myDate.getFullYear();
var month = myDate.getMonth()+1;
var date = myDate.getDate();
createTime = Appendzero(year)+'-'+Appendzero(month)+'-'+Appendzero(date);
// 日期补零
function Appendzero (obj) {
if (obj < 10) return "0" + obj;
else return obj;
}
网友评论