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
//Use this function
if you use the JavaScript style ISO8601 date in JSON, you could use this, from MDN
I know this is a very old thread but I wish to post this to help those who bump into this like I did.
if you don't care about using a 3rd party script, you can use moment,js Then you can use .format() to format it to anything you want it to.
Dates are always a nightmare. Answering your old question, perhaps this is the most elegant way:
With eval we convert our string to javascript code. Then we remove the "/", into the replace function is a regular expression. As we start with new then our sentences will excecute this:
Now, one thing I started using long time ago, is long values that are represented in ticks... why? well, localization and stop thinking in how is date configured in every server or every client. In fact, I use it too in databases.
Perhaps is quite late for this answer, but can help anybody arround here.
The JSON.parse function accepts an optional DateTime reviver function. You can use a function like this:
Then call
And your dates will come out right