时间戳转换为时间秒
function add0(m){return m<10?'0'+m:m }
//时间戳转化成时间格式
function timeFormat(timestamp){
//timestamp是整数,否则要parseInt转换,不会出现少个0的情况
var time = new Date(timestamp);
var year = time.getFullYear();
var month = time.getMonth()+1;
var date = time.getDate();
var hours = time.getHours();
var minutes = time.getMinutes();
var seconds = time.getSeconds();
return year+'-'+add0(month)+'-'+add0(date)+' '+add0(hours)+':'+add0(minutes)+':'+add0(seconds);
}
示例:
前端时间格式2020-11-26T07:48:03.636+0000转化成正常格式
function renderTime(date) {
var dateee = new Date(date).toJSON();
return new Date(+new Date(dateee) + 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '')
}
renderTime()
转换结果为 2020-11-26 15:48:03