Go home Javascript Date, you are drunk
We had a little problem with some Javascript tests, today July 31.
We were doing this:
var date = new Date();
date.setYear(2013);
date.setMonth(5);
date.setDate(13);
The variable date supposed to be "13 Jun 2013" but instead today was "13 Jul 2013".
Why?
Because in Javascript when you do setMonth without specifying the day, Javascript takes by default the current day. Today is 31. June 31 doesn't exists. To correct this overflow, Javascript jumps to the next month.
Thats why it was displaying "13 Jul 2013"
Go home Javascript date. You are drunk.
Written by Tomás
Related protips
4 Responses
![](https://coderwall-assets-0.s3.amazonaws.com/uploads/user/avatar/36790/Screen_Shot_2012-08-27_at_9.12.28_PM.png)
Also months start with 0...which is um...stupid.
![](https://coderwall-assets-0.s3.amazonaws.com/uploads/user/avatar/73272/8da6536c9ddc7a16a9235dfea6c3c09b.png)
i myself prefer to use Moment.js, code became more readable and less buggy
![](https://coderwall-assets-0.s3.amazonaws.com/uploads/user/avatar/32777/seriousmeh-square.jpg)
JS Dates are awesome! XD
Some time ago I discovered the library date.js that seems good to avoid this kind of "unexpected behaviors": http://www.datejs.com
![](https://coderwall-assets-0.s3.amazonaws.com/uploads/user/avatar/55717/3f23a52c7bc87296e96b5a05547da0e0.png)
Yes, I use moment.js too to make things a little easier