1,什么是时间戳
一个能表示一份数据在某个特定时间之前已经存在的、 完整的、 可验证的数据,通常是一个字符序列,唯一地标识某一刻的时间。
说白了就是,表示某一刻的时间;
2,时间戳有什么用;
一般啊,在互联网公司都会在项目种使用时间戳,时间戳主要用于清理缓存,大多数用于版本更新;
3,JS中的具体的时间戳功能的实现
<!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>