毫秒形式的时间戳转时间
第一种:
在main.js
// 时间戳处理
Vue.filter('dateFormat', function (originVal) {
const dt = new Date(originVal*1000);
//获取年份
const y = dt.getFullYear();
//获取月份,月份是从0开始,需要+1,先+1转换成字符串,调用字符串的padStart方法,保证位数为2,不足补0
const m = (dt.getMonth() + 1 + '').padStart(2, '0');
const d = (dt.getDate() + '').padStart(2, '0');
const hh = (dt.getHours() + '').padStart(2, '0');
const mm = (dt.getMinutes() + '').padStart(2, '0');
const ss = (dt.getSeconds() + '').padStart(2, '0');
return `${y}-${m}-${d} ${hh}:${mm}:${ss}`;
})
页面中展示
{{ 时间戳 | dateFormat }}
第二种:
this.newDate = new Date(时间戳 * 1000);
注:第一种为全局可用,但是双向数据绑定时失效,第二种为局部可用,双向数据绑定时也可用
毫秒形式的时间转时间戳
this.newDate = new Date(时间 / 1000);