MVC 4 API控制器中的日期格式

MVC 4 API控制器中的日期格式

问题描述:

我从MVC4 API controller返回JSON响应时遇到了一些问题(使用默认序列化程序).我需要获得像'2013-11-21 08:50:31'这样的数据响应,但它会像'2013-11-22T02:40:28.22499'那样返回.

I am facing some issue while return JSON response from MVC4 API controller(with the use of Default serializer). I need to get data response like '2013-11-21 08:50:31', but it return like '2013-11-22T02:40:28.22499'.

是否可以在序列化之前更改日期格式?

Is it possible to change date format before serializing?

如果将其放在Application_Start()中:

var dateTimeConverter = new IsoDateTimeConverter 
{ 
    DateTimeFormat = "yyyy-MM-dd hh:mm:ss" 
};

GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings
                   .Converters.Add(dateTimeConverter);

然后将以所需的格式输出所有日期.通过 Json.Net IsoDateTimeConverter不起作用.

Then all dates will be output in the desired format. Found through On the nightmare that is JSON Dates. Plus, JSON.NET and ASP.NET Web API and Json.Net IsoDateTimeConverter is not working.