反序列化JSON日期无时间 [英] Deserializing JSON dates timezone-less

查看:166
本文介绍了反序列化JSON日期无时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




  • 我有一个从昨天开始打破我的头脑的问题,我的数据库中有一个包含以下值的日期字段:






  • 然后我的应用程序获取价值并发送到我的网络表单。(值仍然相同:)这很好!!



< img src =https://i.stack.imgur.com/pH1hR.pngalt =enter image description here>




  • 在客户端我放弃了一个javascript警报,看到从JSON 的值(值仍然是一样的:)这很好!!





客户端的警报代码是这样的:

  // transaction jso n model 
var jsonTransaction = @(Html.Raw(Json.Encode(this.Model.Transaction)));
alert(new Date(parseInt(jsonTransaction.Date.substr(6))));




  • 现在当我将值发送到服务器是我得到的






  • 最后在JSON反序列化之后,我的日期时间是错误的!而不是第7天现在第8天





这是反序列化的代码:

  public JsonResult SaveBill(string jsonTransaction,string jsonTranDetails)
{
_appState = this.AppState;
JsonResult returnVal = returnVal = Json(
new {Success = true}
);

var transaction = JsonConvert.DeserializeObject< BillTransaction>(jsonTransaction,new JsonSerializerSettings(){NullValueHandling = NullValueHandling.Ignore});

任何线索如何解决这个问题与日期,我应该得到相同的日期,因为我没有'不要改变任何东西希望有人可以指导我解决问题。



提前感谢

解决方案

Java脚本在将日期解析为currentdate = new Date(123232)
时,使用通用时间,因此当您将客户端发送日期转换为ISO日期如
时,请确保日期在UTC之前将其转换为强。

  return String.Format({0:yyyy-MM-ddTHH:mm:ss.fffZ },dt); 

bty我已经创建了JSON转换为覆盖任何日期序列化到客户端使用上述功能。


I have a problem that is breaking my head since yesterday and don't know how to deal with it.

  • I have a date field in my database that contains the following value:

  • Then my application get the value and send that to my web form. (the value still the same :) thats fine!!

  • In client side I put a break with a javascript alert to see the value that is comming from JSON (the value still the same :) thats fine!!

The code in client side for the alert is this:

// transaction json model
var jsonTransaction = @(Html.Raw(Json.Encode(this.Model.Transaction))); 
alert(new Date(parseInt(jsonTransaction.Date.substr(6))));

  • Now when I send back the value to the server this is what I get

  • And finally after deserialization of the JSON my date time is wrong!! instead of Day 7 its now Day 8???????

This is the code for deserializing:

public JsonResult SaveBill(string jsonTransaction, string jsonTranDetails)
{
    _appState = this.AppState;
    JsonResult returnVal = returnVal = Json(
       new { Success = true }
    );

var transaction = JsonConvert.DeserializeObject<BillTransaction>(jsonTransaction, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore });

Any clue on how to solve this issue with dates, I should get the same date because I didn't change anything. Hope someone can guide me for a solution.

Thanks in advance.

解决方案

Java script use universal time when it parse the date as currentdate = new Date(123232) so when you send date to client convert it to ISO date such as make sure the date is in UTC before convering it to strong .

return String.Format("{0:yyyy-MM-ddTHH:mm:ss.fffZ}", dt); 

bty I already created JSON Converted to override any date serialization to client side to use the above function .

这篇关于反序列化JSON日期无时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆