javascript new Date with string param has wrong date

I am creating a new date from string

var s = "2017-12-06"
var dt = new Date(s)
console.log(dt) // outputs Tue Dec 05 2017 19:00:00 GMT-0500 (EST)

What am I missing ?

Jon Skeet

Date.toString() is formatted in your local time zone, but because you've passed in an ISO-8601 string, the value is parsed as if it's UTC.

From the Date.parse() documentation (as the Date(String) constructor is documented to behave like Date.parse):

The date time string may be in a simplified ISO 8601 format. For example, "2011-10-10" (just date) or "2011-10-10T14:48:00" (date and time) can be passed and parsed. Where the string is ISO 8601 date only, the UTC time zone is used to interpret arguments. If the string is date and time in ISO 8601 format, it will be treated as local.

So you'll end up with a Date which is equivalent to 2017-12-06T00:00:00Z. But Date.toString() shows you that instant in time in your current time zone - and if you're in America/New_York or a similar time zone which is 5 hours behind UTC at that moment in time, that means it'll print December 5th at 7pm.


See more on this question at Stackoverflow