In my Java Script app I have the date stored in a format like so:
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”);
Fri Sep 23 2011 20:00:00 GMT-0400 (Eastern Daylight Time)
Notice that Eastern Daylight Time is
-4 hours and that the hours on the date you’re getting back are
20h + 4h = 24h
which is midnight of 2011-09-24.
You’re getting the right date, you just never specified the correct time zone.
If you need to access the date values, you can use
getUTCDate() or any of the other
var d, days; d = new Date('2011-09-24'); days = ['Sun', 'Mon', 'Tues', 'Wed', 'Thurs', 'Fri', 'Sat']; console.log(days[d.getUTCDay()]);
There are several crazy things that happen with a JS DATE object that convert strings, for example consider the following date you provided
Note: The following examples may or may not be ONE DAY OFF depending on YOUR timezone and current time.
new Date("2011-09-24"); // Year-Month-Day // => Fri Sep 23 2011 17:00:00 GMT-0700 (MST) - ONE DAY OFF.
However, if we rearrange the string format to Month-Day-Year…
new Date("09-24-2011"); => // Sat Sep 24 2011 00:00:00 GMT-0700 (MST) - CORRECT DATE.
Another strange one
new Date("2011-09-24"); // => Fri Sep 23 2011 17:00:00 GMT-0700 (MST) - ONE DAY OFF AS BEFORE. new Date("2011/09/24"); // change from "-" to "/". => // Sat Sep 24 2011 00:00:00 GMT-0700 (MST) - CORRECT DATE.
We could easily change hyphens in your date “2011-09-24” when making a new date
new Date("2011-09-24".replace(/-/g, '\/')); // => "2011/09/24". => // Sat Sep 24 2011 00:00:00 GMT-0700 (MST) - CORRECT DATE.
What if we had a date string like “2011-09-24T00:00:00”
new Date("2011-09-24T00:00:00"); // => Fri Sep 23 2011 17:00:00 GMT-0700 (MST) - ONE DAY OFF.
Now change hyphen to forward slash as before; what happens?
new Date("2011/09/24T00:00:00"); // => Invalid Date
I typically have to manage the date format 2011-09-24T00:00:00 so this is what I do.
new Date("2011-09-24T00:00:00".replace(/-/g, '\/').replace(/T.+/, '')); // => Sat Sep 24 2011 00:00:00 GMT-0700 (MST) - CORRECT DATE.
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.
Get the first month and day of a given year
new Date(2011, 0); // Normal behavior as months in this case are zero based. => // Sat Jan 01 2011 00:00:00 GMT-0700 (MST)
Get the last month and day of a year
new Date((2011 + 1), 0, 0); // The second zero roles back one day into the previous month's last day. => // Sat Dec 31 2011 00:00:00 GMT-0700 (MST)
Example of Number, String arguments. Note the month is March because zero based months again.
new Date(2011, "02"); => // Tue Mar 01 2011 00:00:00 GMT-0700 (MST)
If we do the same thing but with a day of zero, we get something different.
new Date(2011, "02", 0); // again the zero roles back from March to the last day of February. => // Mon Feb 28 2011 00:00:00 GMT-0700 (MST)
Adding a day of zero to any year and month argument will get the last day of the previous month. If you continue with negative numbers you can continue rolling back another day
new Date(2011, "02", -1); => // Sun Feb 27 2011 00:00:00 GMT-0700 (MST)
To normalize the date and eliminate the unwanted offset (tested here : https://jsfiddle.net/7xp1xL5m/ ):
var doo = new Date("2011-09-24"); console.log( new Date( doo.getTime() + Math.abs(doo.getTimezoneOffset()*60000) ) ); // Output: Sat Sep 24 2011 00:00:00 GMT-0400 (Eastern Daylight Time)
This also accomplishes the same and credit to @tpartee (tested here : https://jsfiddle.net/7xp1xL5m/1/ ):
var doo = new Date("2011-09-24"); console.log( new Date( doo.getTime() - doo.getTimezoneOffset() * -60000 ) );
If you want to get hour 0 of some date in the local time zone, pass the individual date parts to the
new Date(2011,08,24); // month value is 0 based, others are 1 based.
Just want to add that apparently adding a space at the end of the string will use UTC for creation.
new Date("2016-07-06") > Tue Jul 05 2016 17:00:00 GMT-0700 (Pacific Daylight Time) new Date("2016-07-06 ") > Wed Jul 06 2016 00:00:00 GMT-0700 (Pacific Daylight Time)
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.
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:
var doo = new Date("2011-09-25 EDT");