一、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();
本文详细介绍了JavaScript中Date对象的setDate()和getDay()方法的使用方式及常见误区,通过实例对比展示了如何正确地设置和获取日期的第一天。
1610

被折叠的 条评论
为什么被折叠?



