Net5.0 修改返回日期格式
//新建DateTimeJsonConverter 类
public class DateTimeJsonConverter : JsonConverter<DateTime> { public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { return DateTime.Parse(reader.GetString()); } public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options) { writer.WriteStringValue(value.ToString("yyyy-MM-dd HH:mm:ss")); } }
// startup写的
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Latest).AddJsonOptions(option => { //格式化返回日期去除T option.JsonSerializerOptions.Converters.Add(new DateTimeJsonConverter()); });