请补全JavaScript代码,实现以下功能:
1. 根据已有的person对象的注册时间求出距离当前时间的天数(天数向下取整)。
2. 将获得的天数和person数据拼接成字符串,作为h2标签的内容。
注意:使用模板字符串进行字符串拼接,字符串最终内容如:尊贵的牛客网2级用户小丽您好,您已经注册牛客网3天啦~
基本思路:因为最终是计算当天距离注册时间的天数,所以将两个时间换成时间戳进行计算更方便
因为获得的时间戳是毫秒级的,所以将时间戳之差换算为天数就是/(1000*60*60*24)
下面介绍几种获得时间戳的方法:
1.Date.parse()
const time = Date.parse(new Date())
2.Math.round()
const time = Math.round(new Date())
3.valueOf()
connst time = (new Date()).valueOf()
4.getTime()
const time = new Date().getTime()
5.数学运算转换
const time1 = +new Date()
const time2 = new Date()*1
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<h2></h2>
<script>
var person = {
level: '2',
name: '小丽',
registTime: '2021-11-01',
}
var h2 = document.querySelector('h2');
// 补全代码
const regDate = new Date(person.registTime).getTime();
const nowDate = new Date(new Date()*1)
const time = nowDate - regDate
const day = time/(1000*60*60*24)
h2.innerHTML = `尊贵的牛客网2级用户小丽您好,您已经注册牛客网${day}天啦~`
</script>
</body>
</html>