When you are using the controller JSON method in ASP.NET MVC or scriptable WCF services, you sometimes have to serialize a .NET
DataContractJsonSerializer will serialize it into the the following format:
There are a few methods:
- “Brutal Force”: Extract the number using a Regular Expression or substring functions and pass it to the
Date object which gets the time in milliseconds in its constructor:
var date = new Date(1235398665390);
Server-side approach: Send to the client side not a .NET
var baseDate = new DateTime(1970, 1, 1);
var currentDate = DateTime.Now.ToUniversalTime();
TimeSpan ts = new TimeSpan(dcurrentDate.Ticks - baseDate.Ticks);
The eval way: On the client side, use the
eval function to evaluate and create the
var date = eval("new " + dateFromTheServer.slice(1, -1));
dateFromTheServer is in the format which was presented at the top of the post.
There are probably other ways which can help you. What is your way?
Gil Fink is an expert in ASP.NET and Microsoft data platform and serves as a Senior Architect at SELA Group. He is a Microsoft data platform MVP and a certified MCPD Enterprise Application Developer. Gil has worked in the past in variety of positions and projects as a leading developer, team leader, consultant and more. His interests include Entity Framework, Enterprise Library, WCF, LINQ, ADO.NET and many other new technologies from Microsoft.
My technical blog: http://www.gilfink.net