Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Why is setMonth(1) giving me March? I believe that 0=Jan, 1=Feb, 2=Mar

<!DOCTYPE html>
<html>
<head>
<script>
window.onload = init;

function init(){
var d = new Date();
d.setFullYear(2014);
d.setMonth(1);
d.setDate(1);

document.getElementById("demo").innerHTML = d;
}
</script>
</head>
<body>

<div id="demo"></div>

</body>
</html>

I get...

Sat Mar 01 2014 15:11:03 GMT-0600 (Central Standard Time)

I'm running Win7 Pro 64-bit and the clock and calendar seem to be correct.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
462 views
Welcome To Ask or Share your Answers For Others

1 Answer

Today is the 31st of January. When you d.setMonth(1); you are trying to set the date to the 31st of February. Since this date doesn't exist, it falls over to the 3rd of March.

Set the whole date when you initialise the object, don't try to change it piecemeal.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...