0

let date = '12 Sep 2021 2:30 PM'

new Date(date).toISOString() returns different values on two different servers -

2021-09-12T09:00:00.000Z

2021-09-12T14:30:00.000Z

Is there any way to resolve this?

  • `new Date(date)` uses the server's local time zone to parse the date. The string that you're providing to it doesn't include the time zone. You could run your node programs by setting the environment variable `TZ=UTC` on all servers so the node processes use UTC, or better yet set the servers' timezones to UTC. – cbr Sep 10 '21 at 11:40
  • 1
    @cbr moreover, it's not even a standard date string, so the parsing will be implementation dependent. It might be that two different versions of Node parse it differently. – VLAZ Sep 10 '21 at 11:41
  • 1
    @VLAZ excellent point, more about that here: [Why does Date.parse give incorrect results?](https://stackoverflow.com/q/2587345/996081) – cbr Sep 10 '21 at 11:42
  • Also: [What are valid Date Time Strings in JavaScript?](https://stackoverflow.com/q/51715259) – VLAZ Sep 10 '21 at 11:43
  • "12 Sep 2021 2:30 PM" has no associated timezone so is parsed as local to an equivalent UTC time. The host system settings are used for the offset, so for each host with a different offset you'll get a different UTC time. E.g. in Pacific/Port_Moresby (+10) it converts to "2021-09-12T04:30:00.000Z", whereas in Asia/Kolkata (+5:30) it converts to "2021-09-12T09:00:00.000Z". – RobG Sep 10 '21 at 12:39

0 Answers0