1.如果是13位时间戳
function getdate() {
var now = new Date(),
y = now.getFullYear(),
m = ("0" + (now.getMonth() + 1)).slice(-2),
d = ("0" + now.getDate()).slice(-2);
return y + "-" + m + "-" + d + " " + now.toTimeString().substr(0, 8);
}
2.如果是10位到秒的
需要乘以1000
<html>
<body>
<script type="text/javascript">
function getdate(stmp) {
var now = new Date(stmp*1000),
y = now.getFullYear(),
m = ("0" + (now.getMonth() + 1)).slice(-2),
d = ("0" + now.getDate()).slice(-2);
return y + "-" + m + "-" + d + " " + now.toTimeString().substr(0, 8);
}
alert(getdate(1547551970))
</script>>
</body>>
</html>
本文提供了两种JavaScript函数,用于将13位和10位时间戳转换为可读的日期格式。对于13位时间戳,直接使用函数getdate()进行转换;对于10位时间戳,先乘以1000再调用getdate()函数进行转换。
960

被折叠的 条评论
为什么被折叠?



