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)
I believe that it has to do with time-zone adjustment. The date you've created is in GMT and the default time is midnight, but your timezone is EDT, so it subtracts 4 hours. Try this to verify:
There are several crazy things that happen with a JS DATE object that convert strings, for example consider the following date you provided
If you provide separate arguments to the Date constructor you can get other useful outputs as described below
Note: arguments can be of type Number or String. I'll show examples with mixed values.
You are using the ISO date string format which, according to this page, causes the date to be constructed using the UTC timezone:
If you format the text differently, such as
"Jan 01 1970"
, then (at least on my machine) it uses your local timezone.