ASP.NET Web API 的 JSON 输出中没有时间的日期 [英] Dates without time in ASP.NET Web API's JSON output

查看:19
本文介绍了ASP.NET Web API 的 JSON 输出中没有时间的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种简单的方法来配置 JSON.NET,以便 some DateTime 字段将在没有时间的情况下进行格式化,而其他 DateTime 字段仍将被格式化用时间格式化?

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?

示例:

{ firstName: 'John', lastName : 'Doe', birthday: '1965-09-23' }

推荐答案

如果您需要仅影响特定字段,请先创建转换器类型:

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))]

这篇关于ASP.NET Web API 的 JSON 输出中没有时间的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆