0

Just recently swapped to MacOS (m1) and when I wanted to run my environment I saw some of the jest tests were failing ( although they are passing on Win Machine ). After a while of debugging I found the spot where it's failing... and it's something, I have never seen.

function convertToTimeZoneInternal(dateTime: string | number | Date, timeZoneName: string): string {
  if (('UTC' === timeZoneName) && (typeof dateTime === 'string') && needsConversionToTimeZone(dateTime)) return dateTime // You can ignore this if condition
  const dt = new Date(dateTime)
  dt.setMinutes(dt.getMinutes() - dt.getTimezoneOffset())
  return new Date(dt.toLocaleString('en-US', { timeZone: timeZoneName })).toISOString()
}

Example inputs here are:
dateTime: "Sat Jan 01 2005 03:00:00 GMT+0100 (Central European Standard Time).
timeZoneName: "Europe/Bratislava".

Everything is working until the last return line, where dt.toLocaleString('en-US', { timeZone: timeZoneName }) is returning for both machines ( Win and MacOs) same result: "1/1/2005, 3:00:00 AM" - You can ignore the .toISOString() function for now. but when I use new Date() on top of this, the Win machine is okay but the MacOS machine is throwing me "Invalid Date" as output.

Am I doing something wrong or any idea why is this not working?

Tried to look into similar threads but they are mostly regarding Safari showing Invalid Date. I have no idea if it's somehow connected, as I already stated I'm new to MacOS. Also in those threads there was always a problem of different formatting input of date ( "-", "/", etc. ) Didn't see anyone having issue with same input values.

Serpo
  • 1
  • 1
    See [What are valid Date Time Strings in JavaScript?](https://stackoverflow.com/q/51715259) - you're using a *non-standard date string*. So what you experience is implementation-dependent behaviour. – VLAZ Mar 16 '23 at 12:18
  • @VLAZ Perfect! Thanks, changing the locale from "en-US" to "sv-SE" which is matching the valid date time format for new Date() did the Magic :) – Serpo Mar 16 '23 at 15:29

0 Answers0