網站找了幾個方案不好使,比如:
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"; } } }
