gpt4 book ai didi

c# - 在 web api 2 中仅序列化 DateTime 的日期部分

转载 作者:太空宇宙 更新时间:2023-11-03 17:14:45 26 4
gpt4 key购买 nike

拥有这个对象:

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; }
}

关于c# - 在 web api 2 中仅序列化 DateTime 的日期部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21914674/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com