在json和Web api之间保留C#datetime本地时间?
我有问题,当我在json对象中有datatime时它会将它转换为C#dateTime中的UTC时区只是想问如何保持本地时间?我可以在web.config文件或geter或setter中设置时区属性,因为我必须可能反对有日期和时间吗? 这是一个类的例子?
public class Patient { public long RecordId { get; set; } public string Username { get; set; } public DateTime Date { get; set; } public bool Deleted { get; set; } public string ModifiedBy { get; set; } public DateTime ModifiedOn { get; set; } public string CreatedBy { get; set; } public DateTime CreatedOn { get; set; } }
更新我尝试使用getter和setter来修复我有这个exception{Cannot evaluate expression because the current thread is in a stack overflow state.}
[System.Web.Http.Route("api/postpatientform")] public HttpResponseMessage PostPatientForm(PatientViewModel form) { using (var db = new AthenaContext()) { try { var form2 = Mapper.Map(form); db.Patient.Add(form2); db.SaveChanges(); var newId = form2.RecordId; foreach (var activity in form.PatientActivities) { activity.PatientId = newId; db.NonPatientActivities.Add(Mapper.Map(activity)); } db.SaveChanges(); } catch (DbEntityValidationException e) { foreach (var eve in e.EntityValidationErrors) { Debug.WriteLine("Entity of type "{0}" in state "{1}" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State); foreach (var ve in eve.ValidationErrors) { Debug.WriteLine("- Property: "{0}", Error: "{1}"", ve.PropertyName, ve.ErrorMessage); } } throw; } } return Request.CreateResponse(HttpStatusCode.Created, null); }
您可以更改序列化程序设置以使用JSON.net序列化程序:
GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings = new JsonSerializerSettings { DateFormatHandling = DateFormatHandling.IsoDateFormat, DateTimeZoneHandling = DateTimeZoneHandling.Unspecified, };
您还可以选择各种日期格式: DateTimeZoneHandling
///
/// Specifies how to treat the time value when converting between string and public enum DateTimeZoneHandling { ///. /// /// Treat as local time. If the Local = 0, ///object represents a Coordinated Universal Time (UTC), it is converted to the local time. /// /// Treat as a UTC. If the Utc = 1, ///object represents a local time, it is converted to a UTC. /// /// Treat as a local time if a Unspecified = 2, ///is being converted to a string. /// If a string is being converted to , convert to a local time if a time zone is specified. /// /// Time zone information should be preserved when converting. /// RoundtripKind = 3 }
你可以配置它。 请参阅: http : //www.newtonsoft.com/json/help/html/SerializeDateTimeZoneHandling.htm
这是一个例子:
public void Config(IAppBuilder app) { var config = new HttpConfiguration(); var jsonFormatter = config.Formatters.OfType().First(); jsonFormatter.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Local; app.UseWebApi(config); }
您可以使用TimeZoneInfo.ConvertTime转换为所需的时区。
查看此方法: https : //msdn.microsoft.com/en-us/library/bb382770(v = vs.110).aspx
上述就是C#学习教程:在json和Web api之间保留C#datetime本地时间?分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/1008633.html