json显示日期带T问题的解决方法

时间:2023-03-09 08:06:47
json显示日期带T问题的解决方法

此问题是由Newtonsoft.Json转换json导致的;

Newtonsoft.Json产生的默认日期时间格式为: IsoDateTimeConverter 格式

解决方法:

需要引用下面的命名空间:

using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

解决代码如下:

//这里使用自定义日期格式,如果不使用的话,默认是ISO8601格式

IsoDateTimeConverter timeConverter = new IsoDateTimeConverter {DateTimeFormat = "yyyy'-'MM'-'dd"};

JsonConvert.SerializeObject(mydt,Formatting.Indented, timeConverter)