Parse YYYY-MM-DD dates using the local timezone

The function bellow will do exactly what you want, so you can pass a string in the format "YYYY-MM-DD" and it will return a Date object in your local time zone.

function yyyymmddToLocalDate(isoString) {
  const [year, month, day] = isoString.split('-');
  return new Date(year, month - 1, day);
}

If you're in Brazil for example and call it yyyymmddToLocalDate('2020-10-07') it will return Wed Oct 07 2020 00:00:00 GMT-0300 (Brasilia Standard Time)


Date.parse behaves as follows:

http://www.ecma-international.org/ecma-262/5.1/#sec-15.9.4.2

The function first attempts to parse the format of the String according to the rules called out in Date Time String Format (15.9.1.15). If the String does not conform to that format the function may fall back to any implementation-specific heuristics or implementation-specific date formats.

http://www.ecma-international.org/ecma-262/5.1/#sec-15.9.1.15

ECMAScript defines a string interchange format for date-times based upon a simplification of the ISO 8601 Extended Format. The format is as follows: YYYY-MM-DDTHH:mm:ss.sssZ

Z is the time zone offset specified as “Z” (for UTC) or either “+” or “-” followed by a time expression HH:mm

The value of an absent time zone offset is “Z”.

So in the first case, since it doesn't fit the Date Time String Format, the implementation-specific parse takes effect (which happens to be based on local time). In the second case, it does fit the DTSF, so it is parsed as if the time zone was unspecified (which is supposed to be UTC), hence the disparity