Customer cs = new Customer();
cs.BirthDay = new DateTime(1980, 1, 1, 12, 0, 0);
cs.Name = "steven";
cs.Sex = true;
JavaScriptSerializer js = new JavaScriptSerializer();
string output = js.Serialize(cs);
Customer cust = js.Deserialize(output, typeof(Customer)) as Customer;
if (cust != null)
{
txtBirthDay.Text = cust.BirthDay.Value.ToString();
}
这样处理时会带来一点问题就是,在进行序列化之后的日期不会是原来的 1980-1-1 12:00:00,而是会根据utc时间得来的一个时间,有可能是1979-12-31 4:00:00,这样在界面进行UI展示的时候会带来一致的问题!
其它处理很简单,序列化之前先把本地时间转换来UTC时间,再在反序列之后,把UTC时间转换为本地时间就OK了,看看更改之后代码:
Customer cs = new Customer();
cs.BirthDay = new DateTime(1980, 1, 1, 12, 0, 0).ToUniversalTime();
cs.Name = "steven";
cs.Sex = true;
JavaScriptSerializer js = new JavaScriptSerializer();
string output = js.Serialize(cs);
Customer cust = js.Deserialize(output, typeof(Customer)) as Customer;
if (cust != null)
{
txtBirthDay.Text = cust.BirthDay.Value.ToLocalTime().ToString();
}
如对本文有疑问,请提交到交流论坛,广大热心网友会为你解答!! 点击进入论坛