使用js将Unix时间戳转换为普通时间

时间:2022-06-05 02:36:45

 

1 
var unixtime=1358932051;
formatTime (time) { 2 let unixtime = time 3 let unixTimestamp = new Date(unixtime * 1000) 4 let Y = unixTimestamp.getFullYear() 5 let M = ((unixTimestamp.getMonth() + 1) > 10 ? (unixTimestamp.getMonth() + 1) : '0' + (unixTimestamp.getMonth() + 1)) 6 let D = (unixTimestamp.getDate() > 10 ? unixTimestamp.getDate() : '0' + unixTimestamp.getDate()) 7 let toDay = Y + '-' + M + '-' + D 8 return toDay 9 }