I have a following piece of JSON:
\/Date(1293034567877)\/
which is a result of this .NET code:
var obj = DateTime.Now;
var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
serializer.Serialize(obj).Dump();
Now the problem I am facing is how to create a Date object from this in JavaScript. All I could find was incredible regex solution (many containing bugs).
It is hard to believe there is no elegant solution as this is all in JavaScrip, I mean JavaScript code trying to read JSON (JavaScript Object Notation) which is supposed to be a JavaScript code and at this moment it turns out it's not cause JavaScript cannot do a good job here.
I've also seen some eval solutions which I could not make to work (besides being pointed out as security threat).
Is there really no way to do it in an elegant way?
Similar question with no real answer:
How to parse ASP.NET JSON Date format with GWT
You can convert JSON Date to normal date format in JavaScript.
As Callum mentioned, for me, the best way is to change the Controller method to string instead of JsonResult".
From the ajax method you can do something like this
AngularJS couldn't parse .NET JSON date
/Date(xxxxxxxxxxxxx)/
string either..I side stepped this issue by formatting the date to its ISO 8601 string representation instead of dumping the
Date
object directly...Here is a sample of ASP.NET MVC code..
I tried
RFC 1123
but it doesn't work.. Angular treats this as string instead of Date.I've not used .Net for things like this. If you were able to get it to print something like the following out it should work.
Note, unless you're parsing that JSON string by some other means or only expect users to have modern browers with a built in JSON parser you need to use a JS framework or JSON2 to parse the JSON string outputted by the server into a real JSON object.
Wiki Link
Link to JSON2 file
Live Example
What's wrong with:
This returns for me "Wed Dec 22 2010 16:16:07 GMT+0000 (GMT Standard Time)".
Or do you need to get the number out the json?