Parsing if strings with the Date constructor is not recommended due to variances between browsers.
In this case, browsers conforming with ES5 and later will parse "2016-09-04T00:00:00" as a "local" date and time for midnight at the start of 4 September, 2016. Some browsers (like Safari) will incorrectly treat it as UTC. See above advice.
However, in all browsers, new Date()
will create a Date for the current instant, not midnight at the start of the day. So if you run this code on 4 September 2016 then unless you run it at exactly 00:00:00.000 local time the time of new Date()
will be after 00:00:00.000.
If you want to compare the two, then set the time part of new Date()
to midnight at the start of the day, e.g.
// Returns false on 5 September, 2016 local time
console.log(new Date('2016-09-05T00:00:00') < new Date().setHours(0,0,0,0));
The above works in most browsers because the <
operator coerces the first Date to its internal time value, and the return from setHours is the adjusted time value of the second Date.
Note
The host system time zone offset should be used in the creation of both dates, so has an equal effect on both so can be ignored in this case.
It is strongly recommended that you do not rely on parsing strings with the Date constructor or Date.parse (they are equivalent for parsing). Always manually parse strings. Use a bespoke function for the format you need to deal with, or use a library and provide the format to parse. That is the only way that you can be sure of correct parsing.