简体   繁体   English

反序列化JSON日期时区少

[英]Deserializing JSON dates timezone-less

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. 然后我的应用程序获取值并将其发送到我的Web表单。 (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!! 在客户端,我暂停了一个javascript警报,看看来自JSON的值(值仍然相同:)那很好!!

在此输入图像描述

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!! 最后在JSON反序列化之后我的日期时间错了! instead of Day 7 its now Day 8??????? 而不是第7天它现在是第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 . Java脚本在将日期解析为currentdate = new Date(123232)时使用通用时间,因此当您向客户端发送日期时将其转换为ISO日期,例如确保日期为UTC,然后再将其转换为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 Converted来覆盖任何日期序列化到客户端以使用上面的函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM