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.
UPDATE
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)
Answer from SoEzPz on Stack OverflowIs the Javascript date object always one day off? - Stack Overflow
typescript - Which time zone does JavaScript `new Date()` use? - Stack Overflow
Date vs new Date in JavaScript - Stack Overflow
new Date vs. new Date() - Is there any difference?
Videos
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.
UPDATE
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)
Notice that Eastern Daylight Time is -4 hours and that the hours on the date you're getting back are 20.
20h + 4h = 24h
which is midnight of 2011-09-24. The date was parsed in UTC (GMT) because you provided a date-only string without any time zone indicator. If you had given a date/time string w/o an indicator instead (new Date("2011-09-24T00:00:00")), it would have been parsed in your local timezone. (Historically there have been inconsistencies there, not least because the spec changed more than once, but modern browsers should be okay; or you can always include a timezone indicator.)
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 getUTC*() functions:
var d,
days;
d = new Date('2011-09-24');
days = ['Sun', 'Mon', 'Tues', 'Wed', 'Thurs', 'Fri', 'Sat'];
console.log(days[d.getUTCDay()]);
JavaScript will use the client's local time but it also has UTC / GMT methods. The following is from Mozilla:
The JavaScript Date object supports a number of UTC (universal) methods, as well as local time methods. UTC, also known as Greenwich Mean Time (GMT), refers to the time as set by the World Time Standard. The local time is the time known to the computer where JavaScript is executed.
While methods are available to access date and time in both UTC and the localtime zone, the date and time are stored in the local time zone:
Note: It's important to keep in mind that the date and time is stored in the local time zone, and that the basic methods to fetch the date and time or its components all work in the local time zone as well.
Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
What time zone does the Javascript new Date() use?
Date objects store the number of milliseconds since The Epoch (Jan 1st 1970 at midnight GMT). They have methods like getDay and getMonth and such that provide values in the local timezone, and also functions like getUTCDay and getUTCMonth that provide values in UTC (loosely, GMT).
If you're parsing a string, you need to be sure that the string is in a format that Date knows how to parse. The only defined format in the specification is a simplified derivative of ISO-8601: YYYY-MM-DDTHH:mm:ss.sssZ. But that was only added in ES5 (and then updated in ES2015 and ES2016). The time zone of the string is determined by whether it has a UTC offset indicator (Z, -08:00, +05:30, etc.) and whether it's date-only or date/time. Here are some examples:
function test(str) {
const dt = new Date(str);
const p = document.createElement("pre");
p.textContent =
`String: "${str}"\n` +
`UTC: ${dt.toISOString()}\n` +
`Local: ${dt.toLocaleString()}`
;
document.body.appendChild(p);
}
// No UTC offset provided, date-only form => parsed as UTC:
test("2015-08-09");
// No UTC offset provided, date/time form => parsed as local time:
test("2015-08-09T09:32:54");
// Has UTC offset "Z" (for "no offset") => parsed as UTC:
test("2015-08-09T09:32:54.427Z");
// Has UTC offset -08:00 => parsed as UTC minus eight hours:
test("2015-08-09T09:32:54.427-08:00");
I don't recommend relying on it, but even though it's not specified, all major JavaScript engines will successfully parse the American-specific formats MM/DD/YYYY, MM/DD/YYYY hh:mm, MM/DD/YYYY hh:mm:ss, or MM/DD/YYYY hh:mm:ss.SSS (although the milliseconds portion is ignored by some). Note the order in the date portion: month, day, year. Having a UTC offset indicator is not broadly supported and will cause an error on most engines, so don't include one. Without one, all of those are parsed as local time (even the date-only one). Note that the date field separator must be / (08/09/2015), not - (08-09-2015).
function test(str) {
const dt = new Date(str);
const p = document.createElement("pre");
p.textContent =
`String: "${str}"\n` +
`UTC: ${dt.toISOString()}\n` +
`Local: ${dt.toLocaleString()}`
;
document.body.appendChild(p);
}
test("08/09/2015");
test("08/09/2015 09:32");
test("08/09/2015 09:32:54");
test("08/09/2015 09:32:54.427");
But again: I don't recommend relying on that.
From the specs:
When
Dateis called as a function rather than as a constructor, it returns a String representing the current time (UTC).
and:
When
Dateis called as part of anewexpression, it is a constructor: it initialises the newly created object.
So, new Date(...) returns an object such that obj instanceof Date is true, whereas Date(...) basically returns the same as new Date().toString().
new Date creates a new Date object that you can modify or initialize with a different date while Date returns a string of the current date/time, ignoring its arguments.
Did my best to google around and found nothing. Anyone have insight? They seem the same but figured it was worth asking.