<!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>
// 方法一
let d1 = '2021-7-14'
let arry = d1.split('-')
let date = new Date(arry[0],arry[1]-1,arry[2])
console.log(date)
// 方法二
let d1 = '2021-7-14'
let arry = d1.split('-')
let date = new Date()
date.setFullYear(arry[0])
date.setMonth(arry[1]-1)
date.getDate(arry[2])
console.log(date)
</script>
</body>
</html>
Date==>字符串
<!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>
//方法一
let date = new Date()
let d = date.toLocaleDateString()
console.log(d)
//方法二
let date = new Date()
let d = `${date.getFullYear()}-${date.getMonth() +1}-${date.getDate()}`
console.log(d)
</script>
</body>
</html>
字符串==>时间戳
<!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>
// 方法一
let d1 = '2021-7-14'
let arry = d1.split('-')
let date = new Date(arry[0],arry[1],arry[2])
let s = date.getTime()
console.log(s)
</script>
</body>
</html>
时间戳==>字符串
<!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>
//方法一
let s = 0
let date = new Date()
date.setTime(s)
console.log(date)
//方法二
let s = 0
let date = new Date()
date.setTime(s)
//输出日期
let d = date.toLocaleDateString()
//输出时间
let t = date.toLocaleTimeString()
console.log(d)
console.log(t)
</script>
</body>
</html>
Date==>时间戳
<!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>
//方法一
let date = new Date()
let s = date.getTime()
console.log(s)
//方法二
let s2 = Date.now()
console.log(s2)
</script>
</body>
</html>
时间戳==>Date
<!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>
//方法一
let s = 0
let date = new Date(s)
console.log(date)
//方法二
let s = 0
let date = new Date()
date.setTime(s)
console.log(date)
</script>
</body>
</html>