Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node.js cannot parse ISOString date?

We store every date data in ISO format using new Date().toISOString().

I tried to convert this ISO formatted date into Date object in node.js but I get Invalid Date response.

date string is 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 wrong character but they are fine and can be converted on browser console.

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

Thanks for reading.

like image 537
Eugene Yu Avatar asked Jul 14 '14 02:07

Eugene Yu


People also ask

What can I use instead of date parse?

If the DATEPARSE function is not available for the data that you're working with, or the field you are trying to convert is a number data type, you can use the DATE function instead.

How do you use toISOString on a date?

toISOString() method is used to convert the given date object's contents into a string in ISO format (ISO 8601) i.e, in the form of (YYYY-MM-DDTHH:mm:ss. sssZ or ±YYYYYY-MM-DDTHH:mm:ss. sssZ). The date object is created using date() constructor.

How do I convert ISO date to local date?

Use the Date() constructor to convert an ISO string to a date object, e.g. new Date('2023-07-21T09:35:31.820Z') . The Date() constructor will easily parse the ISO 8601 string and will return a Date object. Copied!

What is date parse?

Date.parse() The Date.parse() method parses a string representation of a date, and returns the number of milliseconds since January 1, 1970, 00:00:00 UTC or NaN if the string is unrecognized or, in some cases, contains illegal date values (e.g. 2015-02-31).


1 Answers

Try using moment library. It has a lot of functionality to work with dates and can easily be used both on client and server side. Calling moment("2014-07-09T14:00:00.000Z").toDate() would convert your string to a Date JavaScript Object, using this library.

like image 127
Vinicius Teixeira Avatar answered Oct 04 '22 11:10

Vinicius Teixeira