JS Date类型

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script>
// 获取当前日期对象
var date = new Date()
console.log(date) // Sat Jun 04 2022 20:04:17 GMT+0800 (中国标准时间)
// 获取时间片
console.log(date.getFullYear()) // 年
console.log(date.getMonth()) // 月
console.log(date.getDate()) // 日
console.log(date.toString()) // 字符串输出:Sat Jun 04 2022 20:04:36 GMT+0800 (中国标准时间)
console.log(date.toLocaleString()) // 2022/6/4 20:04:59

// 构建日期对象
var date2 = new Date("2012/12/12")
console.log(date2.getFullYear()) // 2012
console.log(date2.getMonth()) // 11
console.log(date2.getDate()) // 12



</script>
</head>
<body>
<h1>welcome to JS!</h1>
</body>
</html>

 

posted @ 2022-06-04 20:08  呼长喜  阅读(21)  评论(0编辑  收藏  举报