In my Java Script app I have the date stored in a format like so:
2011-09-24
Now when I try using the above value to create a new Date object (so I can retrieve the date in a different format), the date always comes back one day off. See below:
var doo = new Date("2011-09-24");
console.log(doo);
logs:
Fri Sep 23 2011 20:00:00 GMT-0400 (Eastern Daylight Time)
Though in the OP's case the timezone is EDT, there's not guarantee the user executing your script will be int he EDT timezone, so hardcoding the offset won't necessarily work. The solution I found splits the date string and uses the separate values in the Date constructor.
Note that you have to account for another piece of JS weirdness: the month is zero-based.
This probably is not a good answer, but i just want to share my experience with this issue.
My app is globally use utc date with the format 'YYYY-MM-DD', while the datepicker plugin i use only accept js date, it's hard for me to consider both utc and js. So when i want to pass a 'YYYY-MM-DD' formatted date to my datepicker, i first convert it to 'MM/DD/YYYY' format using moment.js or whatever you like, and the date shows on datepicker is now correct. For your example
Apparently d1 is what i want. Hope this would be helpful for some people.
nevermind, didn't notice the GMT -0400, wich causes the date to be yesterday
You could try to set a default "time" to be 12:00:00
Just want to add that apparently adding a space at the end of the string will use UTC for creation.
Edit: This is not a recommended solution, just an alternative answer. Please do not use this approach since it is very unclear what is happening. There are a number of ways someone could refactor this accidentally causing a bug.
This through me for a loop, +1 on zzzBov's answer. Here is a full conversion of a date that worked for me using the UTC methods:
Your issue is specifically with time zone. Note part
GMT-0400
- that is you're 4 hours behind GMT. If you add 4 hours to the displayed date/time, you'll get exactly midnight 2011/09/24. UsetoUTCString()
method instead to get GMT string: