JackBrown JackBrown - 1 year ago 97
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??


Answer Source

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


 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/

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download