Date going to 1 April 2013 instead of 31 March 2013
date, javascript
Solution
Month of Date is a number between 0-Jan and 11-Dec,
So 3 is April...
It's extremely annoying because:
- day- 1 to 31. one based index
- month- 0 to 11. zero based index.
Well... javascript's specs... carry on.
MDN
You can use this to set it right:
date.setMonth(parseInt(s[1], 10) - 1);
You can see it works here :
Problem
I have these two functions that creates a new string in the correct format (`mm-dd-yyyy`) but right now it seems to not work so well... when I input the date `31-03-2013` which is a valid date, it comes out with `04-01-2013` as in the first of the month after.... Here are the two functions: ``` Date.prototype.sqlDate = Date.prototype.sqlDate || function () { return this.getMonth() + "-" + this.getDate() + "-" + this.getFullYear(); }; String.prototype.sqlDate = String.prototype.sqlDate || function () { var date = new Date(0); var s = this.split("-"); //If i log "s" here its output is: // ["31", "03", "2013", max: function, min: function] date.setDate(s[0]); date.setMonth(s[1]); date.setYear(s[2]); return date.sqlDate(); }; ```