Javascript New Date() returns invalid date

I have an Array with date strings formatted: "24-Jul-2017".
and when i use new Date("24-Jul-2017"); it returns date with one day offset.
2017-07-23T22:00:00.000Z

Tried different formatting but could not get correct date.

Jon Skeet
people
quotationmark

Assuming the browser is in a time zone with a UTC offset of +2 at the start of July 24th 2017, it's behaving as documented.

The Date constructor is documented as behaving like Date.parse, which then includes this documentation - along with multiple warnings not to use the constructor accepting a string, or the parse method:

Given a date string of "March 7, 2014", parse() assumes a local time zone, but given an ISO format such as "2014-03-07" it will assume a time zone of UTC (ES5 and ECMAScript 2015). Therefore Date objects produced using those strings may represent different moments in time depending on the version of ECMAScript supported unless the system is set with a local time zone of UTC. This means that two date strings that appear equivalent may result in two different values depending on the format of the string that is being converted.

So it's providing you July 24th 2017 at midnight in the local time zone.

It sounds to me like you'd be better off using Moment.js or something similar to give you clearer control over the parsing/formatting.

people

See more on this question at Stackoverflow