I get dates from the database in this format:
yyyy-mm-dd
When I create a javascript Date object using this string, it builds a day before the date.
You can test this in your console:
var d = new Date("2015-02-01");
d
You will get January 31st! I've tested many theories, but none answer the question.
- The day is not zero-based, otherwise it would give Feb 00, not Jan 31
- It's not performing a math equation, subtracting the day from the month and/or year
- Date(2015-02-01) = Wed Dec 31 1969
- Date("2015-01") = Wed Dec 31 2014
- It is not confusing the day for the month
- Date("2015-08-02") = Sat Aug 01 2015
- If this were true the date would be Feb 08 2015
- If you create a Date using a different format, it works fine
- Date("02/01/2015") = Feb 1st, 2015
My conclusion is that js does this purposefully. I have tried researching 'why' but can't find an explanation. Why does js build dates this way, but only with this format? Is there a way around it, or do I have to build the Date, then set it to the next day?
PS: "How to change the format of the date from the db" is not what I'm asking, and that is why I'm not putting any db info here.