简体   繁体   English

Javascript setFullYear

[英]Javascript setFullYear

var currentDate=new Date();
currentDate.setFullYear(2011);
alert(currentDate);

This works, it sets the year to 2011 as expected. 这样行之有效,将年份定为2011年。

alert((new Date()).setFullYear(2011));

This one doesn't work. 这是行不通的。

Any idea why? 知道为什么吗? Am I misunderstanding the syntax? 我是否误解了语法?

When you write alert((new Date()).setFullYear(2011)) , you are calling setFullYear , and passing its return value to alert . 当您编写alert((new Date()).setFullYear(2011)) ,您正在调用setFullYear ,并将其返回值传递给alert

setFullYear returns a timestamp, not the original Date object. setFullYear返回一个时间戳,而不是原始的Date对象。
Therefore, it doesn't do what you want it to. 因此,它不会执行您想要的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM