Node.js can't parse ISOString date?

We save the date data in ISO format using the new Date (). toISOString ().

I tried to convert this date in ISO format to a Date object in node.js, but I got the answer "Invalid answer".

date string isoDate = 2014-07-09T14:00:00.000Z and I did console.log on Date.parse(isoDate); and new Date(isoDate); but each returns NaN and Invalid Date .

I checked if the date string contains any invisible invalid character, but they are beautiful and can be converted to the browser console.

Does this mean that I need to convert the string manually and create a Date object with the parsed string?

Thanks for reading.

+7
javascript date
source share
2 answers

Try using the moment library. It has many functions for working with dates and can be easily used both on the client side and on the server side. The moment("2014-07-09T14:00:00.000Z").toDate() call moment("2014-07-09T14:00:00.000Z").toDate() converts your string into a JavaScript JavaScript object using this library.

+1
source share

I am posting this answer in case someone experiences it like me.

What happened to me, I thought I was sending ISOString from the browser

 { startDate: date.startDate } 

which in fact I was sending an instance of the moment as a parameter

When I checked with the network inspector, I found out that the data being sent is in ISO format - yes, but it is enclosed in double quotation mark ""

 { startDate: "2016-12-31T16:00:00.000Z" } 

it should not be enclosed in double qoutes and should look like this

 { startDate: 2016-12-31T16:00:00.000Z } 

what worked for me is to parse the moment onto iso string

 { startDate: date.startDate.toISOString() } 
0
source share

All Articles