拥有这个对象:
public class Person {
public string FirstName { get; set; }
[DataType(DataType.Date)]
public DateTime Birthday { get; set; }
}
将此对象作为 Web Api 2 中的内容返回会为生日生成此 json:
“2014-02-20T17:00:32.7114097+00:00”
如何让它成为:"2014-02-20" 没有时间部分?
请您参考如下方法:
到目前为止,最简单的方法是继承内置的 IsoDateTimeConverter 类并设置不同的 DateTimeFormat。
public class IsoDateConverter : IsoDateTimeConverter
{
public IsoDateConverter() =>
this.DateTimeFormat = Culture.DateTimeFormat.ShortDatePattern;
}
public class Foo
{
[JsonConverter(typeof(IsoDateConverter))]
public DateTimeOffset Date { get; set; }
}
