您的位置:首页 > Web前端 > JavaScript

javascript的date对象操作笔记

2016-12-02 00:39 323 查看

一、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