1-标准时间转时间戳再转为当前时间
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title></title>
</head>
<body>
<script type="text/javascript">
function gettime() {
let time = new Date();
// 标准时间转为时间戳 只需要一个标准时间,不需要时间戳
let times = time.getTime();
console.log(times);
// 试一下新的方法获取时间戳
let xtimes = Date.now();
console.log(xtimes);
// 获取年份 使用API getFullYear
let year = time.getFullYear();
console.log(`年份是${year}`);
// 获取月份 得+1 getMonth()
let month = time.getMonth() + 1;
// 补0 padstart()
month = String(month).padStart(2,'0');
console.log(`月份是${month}`);
// 获取天 getDate()
let day = time.getDate();
// 补0 padstart()
day = String(day).padStart(2,'0');
console.log(`天是${day}`);
// 获取当前几点 getHours()
let hour = time.getHours();
// 补0 padstart()
hour = String(hour).padStart(2,'0');
console.log(`当前小时是${hour}`);
// 获取当前是多少分钟 getMinutes()
let m = time.getMinutes();
// 补0 padstart()
m = String(m).padStart(2,'0');
console.log(`当前分钟是${m}`);
// 获取当前秒数 getSeconds()
let s = time.getSeconds();
// 补0 padstart()
s = String(s).padStart(2,'0');
console.log(`当前秒是${s}`);
console.log(`当前时间是${year}:${month}:${day}日${hour}:${m}:${s}`);
}
gettime();
// 获取标准时间
let time = new Date();
console.log(time);
// 获取时间戳
let times = time.getTime();
console.log(times);
// 将时间戳毫秒数转为标准时间
time.setTime(times);
console.log(time+'000');
</script>
</body>
</html>
2-时间戳转为毫秒数等
let time = new Date();
console.log(time);
let times = time.getTime();
console.log(`毫秒数是${times}`);
// 转为天数 小时 分钟 秒数 1970/1/1 至今
// 天数 除以1000是将毫秒转为秒
let day = parseInt(times/1000/60/60/24);
// 补0
day < 10 ? '0' : day;
console.log(day);
// 小时
let hour = parseInt(times/1000/60/60);
// 补0
hour < 10 ? '0' : hour;
console.log(`小时是${hour}`);
// 分钟
let m = parseInt(times/1000/60);
// 补0
m < 10 ? '0' : m;
console.log(`分钟是${m}`);
// 秒数
let s = parseInt(times/1000);
// 补0
s < 10 ? '0' : s;
console.log(`秒数是${s}`);
// setTime 是将时间戳转为GMT时间
time.setTime(times);
console.log(time);