工夫戳是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;
}
}
发表回复