JS Date的认识

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Date</title>
</head>
<body>
<script>
var d1 = new Date();
// 首先大印本机上的时间 2018-7-24 11:05:19
console.log(d1.getFullYear());// 2018
console.log(d1.getMonth());// 6,
// 注意是 6,而不是7,所以得到的要加 1 才是实际 月,反之 实际月减1,就是getMonth()的值
console.log(d1.getDate()); // 24, 拿到天数不是 getDay()
console.log(d1.getHours());// 11
console.log(d1.getMinutes());// 5
console.log(d1.getSeconds())// 19

// 再看看小时
d1.setHours(24);
console.log(d1.getHours());
// 得到的是 0,说明 0 对应 24

// 得到年, 设置年
console.log(d1.getFullYear());
d1.setFullYear(2019);
console.log(d1.getFullYear());

// 得到月, 设置月
console.log(d1.getMonth());
// d1.getMonth() 的结果要在实际月加上 1
d1.setMonth(8); // 表示 9 月
d1.setDate(30);
console.log(d1.getMonth());
console.log(d1.getDate());

d1.setDate(31); // 但 9 月没有 31这一天
console.log(d1.getMonth());// 所以 这里变成了 9 , 也就是 10月
console.log(d1.getDate()); // 这里变成 1

d1.setMonth(12);
console.log(d1.getMonth());// 12 + 1 = 13 了, 所以 变为 0

// 得到日 , 设置月, 记住上面月设置的是 12
console.log(d1.getDate());
</script>
</body>
</html>

原文地址:https://www.cnblogs.com/hello-dummy/p/9358935.html