工夫戳是 1970-01-01 开始的所以给初始了这个日期 time 是 时分秒 除 1000 是因为
Date.parse 办法会返回一个 000 结尾的毫秒级工夫戳而且我须要的是秒级
1. 时分秒工夫戳转 HH:mm:ss
times(data) {let date = new Date(data * 1000)
let hh = (date.getHours() < 10) ? ('0' + date.getHours() + ':') : (date.getHours() + ':');
let mm = (date.getMinutes() < 10) ? ('0' + date.getMinutes() + ':') : (date.getMinutes() + ':');
let ss = (date.getSeconds() < 10) ? ('0' + date.getSeconds()) : (date.getSeconds());
return hh + mm + ss;
}
var t=1521694261;
timestampToTime(t)
function timestampToTime(timestamp) {var date = new Date(timestamp * 1000); // 工夫戳为 10 位需 *1000,工夫戳为 13 位的话不需乘 1000
Y = date.getFullYear() + '-';
M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
D = change(date.getDate()) + ' ';
h = change(date.getHours()) + ':';
m = change(date.getMinutes()) + ':';
s = change(date.getSeconds());
return Y + M + D + h + m + s;
}
function change(t) {if (t < 10) {return "0" + t;} else {return t;}
}
2.HH:mm:ss 转工夫戳办法
time_to_sec(time) {if (time !== null) {
let s = "";
s = Date.parse('1970-01-01' + time) / 1000
return s;
}
}