Reputation: 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.
Upvotes: 1
Views: 1529
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
Reputation: 11
Try:
console.log(JSON.stringify(response[x]['time_start']))
Probably this will not return "2022-07-19T00:00:00.0000000Z"
.
Upvotes: 1