自动化日期/时间到日期和时间json c#

时间:2015-12-14 15:12:54

标签: c# json datetime automapper

日期/时间到日期和时间字段。

DB Class

    public DateTime ValidFrom { get; set; }

VM类

    public DateTime ValidFromDate { get; set; }
    public TimeSpan ValidFromTime { get; set; }

我尝试了自动化:

推出

.ForMember(dest => dest.ValidFromDate, src => src.MapFrom(m => m.ValidFrom.ToString("dd/MM/yyyy")) 
.ForMember(dest => dest.ValidFromTime, src => src.MapFrom(m => m.ValidFrom.TimeOfDay))

拉入

.ForMember(dest => dest.ValidFrom, src => src.MapFrom(m =>
DateTime.Parse(m.ValidFromDate.Contains("T") ?
m.ValidFromDate.Remove(m.ValidToDate.LastIndexOf("T",
StringComparison.Ordinal) + 1) : m.ValidFromDate + " " +
m.ValidFromTime)));

然而,这会引发错误,日期时间无法识别

我注意到我驾驶这样做:GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.Converters.Add(new Newtonsoft.Json.Converters.IsoDateTimeConverter { DateTimeFormat = "dd-MM-yyyy" });

但是想知道我是如何处理时间的?

0 个答案:

没有答案