0

I have a JSON file with date format in the form of 2021-09-21 15:37:29.590 +03:00.

How can we convert it to a Date in a T-Z Format ?

FObersteiner
  • 16,957
  • 5
  • 24
  • 56
JAN
  • 20,056
  • 54
  • 170
  • 290

1 Answers1

2

You can pass it into the Date constructor and call toISOString.

const convert = (dateString) => new Date(dateString).toISOString();

console.log(convert('2021-09-21 15:37:29.590 +03:00')); // 2021-09-21T12:37:29.590Z
Mr. Polywhirl
  • 35,562
  • 12
  • 75
  • 123
  • In Safari throws "RangeError: Invalid Date". See [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) – RobG Oct 05 '21 at 20:12