0

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?

2
  • are you getting response client side or server side? Commented Nov 22, 2013 at 11:35
  • Server side response. Commented Nov 22, 2013 at 11:37

2 Answers 2

3

If you put this in your Application_Start():

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

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

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.

Sign up to request clarification or add additional context in comments.

Comments

0

The answer by CodeCaster works great but be aware that with the format hh:mm:ss the time 15:10:10 will be shown as 03:10:10.

To show it as 15:10:10 use this format string instead: "yyyy-MM-dd HH:mm:ss".

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.