Home > Mobile >  Invalid date from ISO format JS
Invalid date from ISO format JS

Time:07-25

I am getting the ISO date from an API call and this is the format

"2022-07-19T00:00:00.0000000Z"

when I try to convert this string into a date it gives "invalid date"

 var d = new Date(JSON.stringify(response[x]['time_start']));

what could be the issue? i tried solutions from similar questions but still no luck, any help would be greatly appreciated.

CodePudding user response:

As per comment answer: just remove JSON.stringify.

const dateString = "2022-07-19T00:00:00.0000000Z";
 
// Valid:
const d = new Date(dateString); // Tue Jul 19 2022 03:00:00 GMT 0300 ...

// Invalid:
const jsonDate = JSON.stringify(dateString); // ""2022-07-19T00:00:00.0000000Z""
const d1 = new Date(jsonDate); // Invalid Date

CodePudding user response:

Try:

console.log(JSON.stringify(response[x]['time_start']))

Probably this will not return "2022-07-19T00:00:00.0000000Z".

  • Related