js如何将时间戳转换为时间
js如何将时间戳转换为时间?用原生js封装一个formatDate函数,传入时间戳就可以转换为时间,格式为年月日时分秒,实例代码如下:
function formatDate (d) {
var now = new Date(parseFloat(d));
var year=now.getFullYear();
var month=now.getMonth()+1;
var date=now.getDate();
if (month >= 1 && month <= 9) {
month = "0" + month;
}
if (date >= 0 && date <= 9) {
date = "0" + date;
}
var hour=now.getHours();
var minute=now.getMinutes();
var second=now.getSeconds();
if (hour >= 1 && hour <= 9) {
hour = "0" + hour;
}
if (minute >= 0 && minute <= 9) {
minute = "0" + minute;
}
if (second >= 0 && second <= 9) {
second = "0" + second;
}
return year+"-"+month+"-"+date+" "+hour+":"+minute+":"+second;
}
let date = formatDate(1656630582329)
console.log(date) //2022-07-01 07:09:42说明:实例是指定时间戳,也可以用+ new Date()获得当前最新的时间戳。
除注明外的文章,均为来源:老汤博客,转载请保留本文地址!
原文地址:https://tangjiusheng.cn/web/ask/505.html
原文地址:https://tangjiusheng.cn/web/ask/505.html
