JSon result and datetime in ASP.NET MVC and WebAPI
In ASP.NET MVC you can return a number of different results from your controller, one of them is JSonResult. It automatically performs object serialization to JSon format and pushes serialized object to requestor. It works pretty good overall but there’s a problem with DateTime values. Consider the following controller class:
and the model is:
Result of the controller Index
action looks like that:
Note that OriginalValue
format is not the first thing you would expect to be returned, right?
People are doing all sort of things to fight this off, see stack overflow discussion here
- regex parsing
- implementing custom result classes, deriving from JsonResult
- custom libraries, for example taking advantage of
$.parseJSON
from JQuery
It will all work, just pick your poison. My recent choice was to return string version of the date and parse it back on the client side.
What’s also interesting, ASP.NET WebAPI controller
will return that: