Click here to Skip to main content
Click here to Skip to main content

Quick Tip – Converting JSON Serialized .NET DateTime to JavaScript Date

, 1 Dec 2011
Rate this:
Please Sign up or sign in to vote.
Converting JSON serialized .NET DateTime to JavaScript Date.

Quick Tip – Converting JSON Serialized .NET DateTime to JavaScript Date

When you are using the controller JSON method in ASP.NET MVC or scriptable WCF services, you sometimes have to serialize a .NET DateTime property. DataContractJsonSerializer will serialize it into the the following format:

/Date(1235398665390)/ 

This format indicates to the client side parsers that the data fragment that was sent is a date representation. But how can you convert it to a JavaScript Date object?

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 DateTime but the total milliseconds and then use the same constructor from the previous bullet. You should pay attention that JavaScript’s base date is 1/1/1970. The following code can help:
  • var baseDate = new DateTime(1970, 1, 1); 
    var currentDate = DateTime.Now.ToUniversalTime(); 
    TimeSpan ts = new TimeSpan(dcurrentDate.Ticks - baseDate.Ticks); 
    return ts.TotalMilliseconds;
  • The eval way: On the client side, use the eval function to evaluate and create the Date object:
  • var date = eval("new " + dateFromTheServer.slice(1, -1));

    Where 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?

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Gil Fink
Architect Sela Group
Israel Israel
Gil Fink is an expert in web development and Microsoft web and data platforms. He works as a senior consultant and architect at Sela Group. He is currently consulting for various enterprises and companies, where he helps to develop Web and RIA-based solutions. He conducts lectures and workshops for individuals and enterprises who want to specialize in infrastructure and web development. He is also a co-author of several Microsoft Official Courses and training kits and the founder of Front-End.IL meetup. You can read his publications at his website: http://www.gilfink.net
Follow on   Twitter   Google+

Comments and Discussions

 
Question"Brutal Force" is better PinmemberSergejsvit6-Dec-11 21:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140718.1 | Last Updated 1 Dec 2011
Article Copyright 2011 by Gil Fink
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid