Newtonsoft--自定义格式化日期
阅读原文时间:2023年07月13日阅读:1

public string ToJson(object obj, string timeFormat)
{
try
{
IsoDateTimeConverter timeConverter = new IsoDateTimeConverter();
timeConverter.DateTimeFormat = timeFormat;

            List<JsonConverter> converters = new List<JsonConverter>();  
            converters.Add(timeConverter);

            JsonSerializerSettings jSetting = new JsonSerializerSettings  
            {  
                //排空  
                //NullValueHandling = NullValueHandling.Ignore,  
                //时间格式化设置  
                Converters = converters  
            };

            string json = JsonConvert.SerializeObject(obj, Formatting.Indented, jSetting);  
            return json;  
        }  
        catch (Exception e)  
        {  
            return "";  
        }  
    }