时间戳转日期格式
程序员文章站
2022-05-27 16:11:54
...
时间戳:1525689770
转换结果:2018-05-07 18:42JS转换方法封装:
/**
* @param timestamp(例时间戳:1525689770)
* @returns {string}(例转换结果:2018-05-07 18:42)
*/
function format_conversion(timestamp) {
var date = new Date();
date.setTime(timestamp * 1000);
var y = date.getFullYear();
var m = date.getMonth() + 1;
m = m < 10 ? ('0' + m) : m;
var d = date.getDate();
d = d < 10 ? ('0' + d) : d;
var h = date.getHours();
h = h < 10 ? ('0' + h) : h;
var minute = date.getMinutes();
var second = date.getSeconds();
minute = minute < 10 ? ('0' + minute) : minute;
second = second < 10 ? ('0' + second) : second;
return y + '-' + m + '-' + d+' '+h+':'+minute;
};