C# WebAPI中DateTime类型字段在使用微软自带的方法转json格式后默认含T的解决办法

博客 分享
0 920
张三
张三 2021-08-09 00:23:36
悬赏:0 积分 收藏

其实网上已经有很多说的了,都是再WebApiConfig添加关于时间的配置

下面是代码

     GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.Converters.Add(  
    new Newtonsoft.Json.Converters.IsoDateTimeConverter()  
    {  
        DateTimeFormat = "yyyy-MM-dd hh:mm:ss"  
    }  
);  
或者
 //格式化json数据中的时间的T
            var jsontimeset = GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings;
            jsontimeset.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Local;
            jsontimeset.DateFormatString = "yyyy-MM-dd HH:mm:ss";

上面的方法都行
但是他们就是没说最重要的,得引用这个配置才行啊,在控制器中返回 Json 时间引用

Json(new { total = total, rows = rows }, GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings)

回帖
    张三

    张三 (王者 段位)

    1111 积分 (2)粉丝 (41)源码

     

    温馨提示

    亦奇源码

    最新会员