简体   繁体   中英

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. (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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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