Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dates without time in ASP.NET Web API's JSON output

Is there a simple way to configure JSON.NET so that some DateTime fields will be formatted without time and other DateTime fields will still be formatted with time?

Example:

{ firstName: 'John', lastName : 'Doe', birthday: '1965-09-23' } 
like image 982
RooSoft Avatar asked May 01 '13 15:05

RooSoft


1 Answers

If you need this to only affect a particular field, create a converter type first:

public class OnlyDateConverter : IsoDateTimeConverter {     public OnlyDateConverter()     {         DateTimeFormat = "yyyy-MM-dd";     } } 

and then add this attribute to whatever fields/properties you want this for:

[JsonConverter(typeof(OnlyDateConverter))] 
like image 178
Youssef Moussaoui Avatar answered Oct 02 '22 21:10

Youssef Moussaoui