js 将13位时间戳转化为YYYY-MM-dd HH:mm:ss

方法一:

var date = 1553677259000,y,m,d,h,mm,s;
date = new Date(ele.createTime);
y = 1900 + date.getYear();
m = "0" + (date.getMonth()+1);
d = "0" + date.getDate();
h = "0" + date.getHours();
mm = "0" +  date.getMinutes();
s = "0" +  date.getSeconds();
date = y + "-" + m.substring(m.length-2, m.length) + "-" + d.substring(d.length-2, d.length) + " " + h.substring(h.length-2, h.length) + ":" + mm.substring(mm.length-2, mm.length) + ":" + s.substring(s.length-2, s.length);
console.log(date);                 //  输出结果: 2019-03-27 17:00:59

当然也可以:
方法二:

var date = 1553677259000,y,m,d,h,mm,s;
date = new Date(ele.createTime);
y = date.getFullYear();
m = date.getMonth()+1;
d = date.getDate();
h = date.getHours();
mm = date.getMinutes();
s = date.getSeconds();
m = m < 10 ? "0" + m : m;
d = d < 10 ? "0" + d : d;
h = h < 10 ? "0" + h : h;
mm = mm < 10 ? "0" + mm : mm;
s = s < 10 ? "0" + s : s;
date = y + "-" + m + "-" + d + " " + h + ":" + mm + ":" + s;
console.log(date);                 //  输出结果: 2019-03-27 17:00:59

你可能感兴趣的:(JS)