后端返回时间戳,前端展示
上代码:
<template>
<div class="history_warp" v-for="(item,i) in historyData" :key="i">
<p>
//1. 把后端返回的时间戳,以传参的形式传到方法里
<span class="history_warp_right_span">{{ hisTime(item.createTime) }}</span>
</p>
</div>
</template>
<script>
export default {
data() {
return {
}
},
methods: {
// 2.把时间戳整合之后,一起 return 返回
hisTime(i){
let timec = new Date(i);
let year = timec.getFullYear(); //年
let month = timec.getMonth() + 1; //月
let date = timec.getDate(); //日
let hours = timec.getHours(); //时
let minutes = timec.getMinutes(); //分
let second = timec.getSeconds(); //秒
month = checkTime(month);
date = checkTime(date);
hours = checkTime(hours);
minutes = checkTime(minutes);
second = checkTime(second);
function checkTime(i) {
if (i < 10) {
i = "0" + i;
}
return i;
}
return year + "-" + month + "-" + date + " " + hours + ":" + minutes + ":" + second;
},
}
}
</script>
此图可解决你的迷惑: