Reputation: 5424
I have a datetime in SQL server that i return from a controller method in my MVC project using return json()
I get this format in my json-response:
time: "/Date(1409763303817)/"
I try to use this data in a table in my UI with this code:
$("#missingTime").html(new Date(data3.time).toDateString());
I get "Invalid Date" in my column.
What am i doing wrong?
Edit: Found a solution
new Date(parseInt(data3.time.replace("/Date(", "").replace(")/",""), 10)
Upvotes: 1
Views: 4856
Reputation: 1986
If you take use of the MVC-Model the Controller-Layer should convert the time into a format the View can handle.
See, a Time is more than Minutes, Hours and Seconds. Its depends on the Timezone the calling Browser physicaly is.
Upvotes: 0
Reputation: 536
The JSON value you have is not a valid number to be parsed into a JavaScript Date object. A quick fix would be to strip out the UTC value (the numbers) from your string using a regular expression and pass this to your function (after parsing into a number), like so:
var regEx = /\d+/g;
var utcInfo = data3.time.match(regEx);
$("#missingTime").html(new Date(parseInt(utcInfo)).toDateString());
Although you might want to check why your JSON response is giving you the incorrect value in the first place. The value in the JSON object needs to be as follows in order for your JS code to work:
time: 1409763303817
Upvotes: 2