javascript/js将json的日期时间,如/Date(1735107993000)/ 转换成日期 或时间
javascript/js将json的日期时间,如/Date(1735107993000)/ 转换成日期 或时间
·
<script>
//时间戳转日期,如:2024-12-25
function timestampToDateString(dateTime) {
//console.log("dateTime", dateTime);
if (dateTime) {
var timestampStr = dateTime.replace("/Date(", "").replace(")/", "");//将 /Date(1735107000000)/ 变成 1735107000000
var timestamp = parseInt(timestampStr);//字符串转long
const date = new Date(timestamp);
//return date.toLocaleString().replace(" 00:00:00", "");
var str = date.toLocaleString().replace(" 00:00:00", "");
return str.replace("/", "-").replace("/", "-");
}
else {
return "";
}
}
//时间戳转日期时间,如:2024-12-25 14:45:25
function timestampToDateTimeString(dateTime) {
//console.log("dateTime", dateTime);
if (dateTime) {
var timestampStr = dateTime.replace("/Date(", "").replace(")/", "");//将 /Date(1730044800000)/ 变成 1730044800000
var timestamp = parseInt(timestampStr);//字符串转long
var date = new Date(timestamp);
Y = date.getFullYear() + '-';
M = ((date.getMonth() + 1) < 10 ? '0' + (date.getMonth() + 1) : (date.getMonth() + 1)) + '-';
D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' ';
h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
return Y + M + D + h + m + s;
}
else {
return "";
}
}
</script>
更多推荐
所有评论(0)