Having this object :
public class Person {
public string FirstName { get; set; }
[DataType(DataType.Date)]
public DateTime Birthday { get; set; }
}
Returning this object as content in Web Api 2 generates this json for Birthday :
"2014-02-20T17:00:32.7114097+00:00"
How can I make it to be : "2014-02-20"
without the time part?
By far the simplest method is to subclass the built in IsoDateTimeConverter
class and set a different DateTimeFormat
.
public class IsoDateConverter : IsoDateTimeConverter
{
public IsoDateConverter() =>
this.DateTimeFormat = Culture.DateTimeFormat.ShortDatePattern;
}
public class Foo
{
[JsonConverter(typeof(IsoDateConverter))]
public DateTimeOffset Date { get; set; }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With