<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<script>
const date = new Date("1996-6-6 08:22:12");
console.log(date * 1);
console.log(Number(date));
console.log(date.valueOf());
console.log(date.getTime());
const timestamp = date.valueOf();
console.log(timestamp);
console.log(new Date(timestamp));
function dateFormat(date, format = "YYYY-MM-DD HH:mm:ss") {
const config = {
YYYY: date.getFullYear(),
MM: date.getMonth() + 1,
DD: date.getDate(),
HH: date.getHours(),
mm: date.getMinutes(),
ss: date.getSeconds(),
};
for (const key in config) {
console.log(key, config[key]);
format = format.replace(key, config[key]);
}
return format;
}
console.log(dateFormat(date));
//传入自己想要的格式
console.log(dateFormat(date, 'YYYY年MM月DD日'));
</script>
</body>
</html>
js 日期时间格式自定义
于 2022-02-26 23:06:26 首次发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)