js 时间戳转日期格式
问题描述
后台保存的时间字段是以时间戳格式保存的,前台展现需要转为普通日期格式。
转换函数如下:
// 将时间戳转为普通日期格式
function transferTimestamp2DateFormat(timestamp) {
var date = new Date(timestamp);
var year = date.getFullYear();
var month = date.getMonth() + 1;
var day = date.getDate();
var hour = date.getHours();
var minute = date.getMinutes();
var second = date.getSeconds();
return year + "-" + checkTime(month) + "-" + checkTime(day) + " "
+ checkTime(hour) + ":" + checkTime(minute) + ":" + checkTime(second);
}
// 补0
function checkTime(time) {
if (time < 10) {
time = "0" + time;
}
return time;
}
该方法返回的格式如下:
2017-09-09 22:22:22