jinx
jinx

Reputation: 25

Invalid date from ISO format JS

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.

Upvotes: 1

Views: 1529

Answers (2)

Sergey Sosunov
Sergey Sosunov

Reputation: 4600

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

Upvotes: 1

stmprmfa
stmprmfa

Reputation: 11

Try:

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

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

Upvotes: 1

Related Questions