views:

72

answers:

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

This works, it sets the year to 2011 as expected.

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

This one doesn't work.

Any idea why? Am I misunderstanding the syntax?

+3  A: 

When you write alert((new Date()).setFullYear(2011)), you are calling setFullYear, and passing its return value to alert.

setFullYear returns a timestamp, not the original Date object.
Therefore, it doesn't do what you want it to.

SLaks
setFullYear() returns a timestamp.
Boris Guéry
@Boris: Thanks; I didn't know that.
SLaks