js时间、日期、时间戳之间的相互转换,批量转换
js批量时间戳数组转时间
这里我只转了时和分,如果还需要其他的年月日之类的,直接在后面加即可。
var leftArr = [1523618433000, 1302693633000, 1623618433000];
var arrs = [];
for (var i = 0; i < leftArr.length; i++) {
let time = new Date(leftArr[i]);
let hours = time.getHours() + 1;
let minutes = time.getMinutes() + 1;
if (hours < 10) hours = "0" + hours
if (minutes < 10) minutes = "0" + minutes
arrs[i] = (hours + ':' + minutes)
}
console.log(arrs)
打印结果:

单个日期转时间戳
这只是将年月日转了时间戳,时分秒自动补0了
let times= new Date("2021-01-09").getTime()
console.log(times)

单个时间戳转时间
let date = new Date(时间戳);
let y = date.getFullYear();
let MM = date.getMonth() + 1;
MM = MM < 10 ? ('0' + MM) : MM;
let d = date.getDate();
d = d < 10 ? ('0' + d) : d;
return y + '-' + MM + '-' + d;
//时间戳转年月日
let sjday=new Date(时间戳).toLocaleDateString().split('/');
sjday[1].length==1?sjday[1]="0"+sjday[1]:sjday[1];
sjday[2].length==1?sjday[2]="0"+sjday[2]:sjday[2];
console.log(sjday.join('-'))
