保持JSON和Web API之间的C#日期时间本地时间? [英] keep C# datetime local time between json and Web api?
问题描述
当我在json对象中有datatime时遇到问题,它将把它转换为C#dateTime中的UTC时区,只是想问如何保持本地时间?我可以在web.config文件或geter或setter中设置时区属性,因为我必须反对有日期和时间吗?
这是类示例?
I have problem when I have datatime in json object it will convert it to UTC time zone in C# dateTime just want to ask how to keep local time?can I set time zone property in web.config file or geter or setter because I have to may object have day and time? this is class example ?
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来修复我有此异常 {由于当前线程处于堆栈溢出状态而无法求值。}
update I tried to use getter and setter to fix I have this 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<Patient>(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<PatientActivity>(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<Patient>(HttpStatusCode.Created, null);
}
推荐答案
您可以更改序列化程序设置使用JSON.net序列化器:
You can change your serializer settings to use the JSON.net serializer :
GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings =
new JsonSerializerSettings
{
DateFormatHandling = DateFormatHandling.IsoDateFormat,
DateTimeZoneHandling = DateTimeZoneHandling.Unspecified,
};
您还可以选择多种日期格式: DateTimeZoneHandling
/// <summary>
/// Specifies how to treat the time value when converting between string and <see cref="DateTime"/>.
/// </summary>
public enum DateTimeZoneHandling
{
/// <summary>
/// Treat as local time. If the <see cref="DateTime"/> object represents a Coordinated Universal Time (UTC), it is converted to the local time.
/// </summary>
Local = 0,
/// <summary>
/// Treat as a UTC. If the <see cref="DateTime"/> object represents a local time, it is converted to a UTC.
/// </summary>
Utc = 1,
/// <summary>
/// Treat as a local time if a <see cref="DateTime"/> is being converted to a string.
/// If a string is being converted to <see cref="DateTime"/>, convert to a local time if a time zone is specified.
/// </summary>
Unspecified = 2,
/// <summary>
/// Time zone information should be preserved when converting.
/// </summary>
RoundtripKind = 3
}
这篇关于保持JSON和Web API之间的C#日期时间本地时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!