ASP.NET Core webapi json 返回时间格式问题
网站找了几个方案不好使,比如:
1:
services.AddMvc().AddJsonOptions(opt => { opt.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm"; });
2:
JsonSerializerSettings settings = new JsonSerializerSettings(); settings.DateTimeZoneHandling = DateTimeZoneHandling.Local; settings.DateFormatString = "yyyy-MM-dd HH:mm:ss"; return Json(new { status = "001", message = "操作成功", data = lsitems });
于是用梯子在一个国外博客上看到一个方案,解决了,如下
[DataType(DataType.Date)] [JsonConverter(typeof(JsonDateConverter))] [Required] public DateTime? StartDate { get; set; }
using Newtonsoft.Json.Converters; namespace AutomatedInvoiceGenerator.Models { class JsonDateConverter : IsoDateTimeConverter { public JsonDateConverter() { DateTimeFormat = "yyyy-MM-dd HH:mm:ss"; } } }