原生 js、javaScript 处理十位数(带有小数点的浮点数时间戳)、十三位数、时间戳, 将时间戳转换成 年、月、日、时、分、秒、毫秒、正常的日期格式

function timestampToTime(timestamp) {

   var date = new Date(timestamp * 1000);//时间戳为10位需*1000,时间戳为13位的话不需乘1000

   var YY = date.getFullYear() + '-';

   var MM = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';

   var DD = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate()) + ' ';

   var hh = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';

   var mm = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';

   var ss = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()) + '.';

   var ms = date.getUTCMilliseconds();

   return YY + MM + DD + ' ' + hh + mm + ss + ms;
}


timestampToTime(1661948221.049084);

console.log(timestampToTime(1661948221.049084)) // 2022-08-31 20:17:1.49

猜你喜欢

转载自blog.csdn.net/CSDN_33901573/article/details/126637008
今日推荐