Newtonsoft--自定义格式化日期

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

  

原文地址:https://www.cnblogs.com/zhoushangwu/p/11490532.html