一、setDate()和getDay()
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>副本</title>
</head>
<body>
<script type="text/javascript">
console.log("/")
var now1 = new Date();
console.log(now1)
console.log(typeof(now1))
console.log("/")
var now2 = new Date();
var now3 = now2.setDate(1);//对now2操作后返回值为number
console.log(now3)
console.log(typeof(now3))
console.log(now2.getDay())
console.log("")
var now4 = new Date()
now4.setDate(1) //对now4直接操作,得到now4还是object
console.log(now4)
console.log(typeof(now4))
</script>
</body>
</html>
得到的结果是:
所以:
var now = new Date();
var firstDay = now.setDate(1).getDay();
会提示错误:Uncaught TypeError: now.setDate(…).getDay is not a function
正确写法是:
var now = new Date();
now.setDate(1);
var firstDay = now.getDay();