前端时间格式转换,js时间戳转时间(年-月-日 时:分:秒),时分秒时间戳转HH:mm:ss
文章目录
时间戳是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;
}
}
评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论