JackBrown JackBrown - 29 days ago 9
Javascript Question

Convert string to javascript date

I have a string that I retrieve from a JSON object and I am trying to cast that to a javascript date variable as given below but its returning an Invalid date all the time. Any reasons why?

jsonObj["dateValue"]; //has a value: 2016-11-04T08:08:42.5780021+00:00
var dateValue = new Date(jsonObj["dateValue"]); // returns invalid date??


JsFiddle

Answer

You have the jsonObj as an array not an object. The first value in the array is your actual object.

try:

 var dateValue = new Date(jsonObj[0]["dateValue"]);

Or simplify your jsonObj value by making it the object, not a wrapping array.

Updated fiddle - https://jsfiddle.net/wyqzmhz1/

Comments