

红框选中的部分在JAVA里是时间格式,传到jsp引入js就变成这个了 百度这个格式叫时间戳,如何将时间戳变回时间格式例如2021-08-01 12:12:12


红框选中的部分在JAVA里是时间格式,传到jsp引入js就变成这个了 百度这个格式叫时间戳,如何将时间戳变回时间格式例如2021-08-01 12:12:12
规定格式转换
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
</body>
<script>
//时间戳转换方法 date:时间戳数字
function formatDate(date) {
var date = new Date(date);
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());
return YY + MM + DD + " " + hh + mm + ss;
}
console.log(formatDate(1403058804));
</script>
</html>