Search code examples
javascriptnode.jsdatedatetimeiso8601

Convert Date without "T" and "Z" to Date with T-Z Format


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 ?


Solution

  • 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