const timestamp = Date.parse(new Date()) //1528292272000
- 转换结果格式2018-6-6 21:24:10
const newTime = (new Date(timestamp).toLocaleDateString().replace(///g, "-") + " " + new Date(timestamp).toTimeString().substr(0, 8));
console.log(newTime)//2018-6-6 21:24:10
- 转换结果格式2018-06-06 21:37:52 (小于10加0)
function timestampToTime(timestamp) {
var date = new Date(timestamp);//时间戳为10位需*1000,时间戳为13位的话不需乘1000
Y = date.getFullYear() + '-';
M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
D = (date.getDate()<10?'0'+(date.getDate()):date.getDate()) + ' ';
h = (date.getHours()<10?'0'+(date.getHours()):date.getHours())+ ':';
m = (date.getMinutes()<10?'0'+(date.getMinutes()):date.getMinutes()) + ':';
s = (date.getSeconds()<10?'0'+(date.getSeconds()):date.getSeconds());
return Y + M + D + h + m + s;
}
console.log(timestampToTime(timestamp)) //2018-06-06 21:37:52