转化结果:
const change = (t) => {
if (t < 10) {
return “0” + t;
} else {
return t;
}
}
const timestampTo2 = (timestamp, ss) => {
let date = new Date(timestamp); //时间戳为10位需*1000,时间戳为13位的话不需乘1000
let Y = date.getFullYear();
let M = (date.getMonth() + 1 < 10 ? ‘0’ + (date.getMonth() + 1) : date.getMonth() + 1);
let D = change(date.getDate());
let h = change(date.getHours());
let m = change(date.getMinutes());
let s = change(date.getSeconds());
if(ss == ‘YMDHMS’){
return Y + ‘-’ + M + ‘-’ + D + ’ ’ + h + ‘:’ + m + ‘:’ + s;
}else if(ss == ‘YMDHM’){
return Y + ‘-’ + M + ‘-’ + D + ’ ’ + h + ‘:’ + m;
}else if(ss == ‘YM’){
return Y + ‘-’ + M ;
}else{
return Y + ‘.’ + M + ‘.’ + D;
}
}
相关文章: