Jump to content
Xtreme .Net Talk

DateTime is less 8 hours after serializing & deserializing


Recommended Posts

Posted

i have a page that sends to a webapi via WebClient which has a DateTime value as one of the parameter, for some reason after serializing it with JavaScriptSerializer it takes -8 out of the value

 

here's what i tested in C#

 

public class MyParam
{
public string param1 { get; set; }
public DateTime param2 { get; set; }
}

 

JavaScriptSerializer serializer = new JavaScriptSerializer();
string json = serializer.Serialize(param); // param = MyParam
json = serializer.Serialize(json);

MyParam test = serializer.Deserialize<MyParam>(json); // less 8 hours of original value

slow down when you need to hurry, stop when you need to move on,

look back when you need to forget, or you might slip and leave sanity

  • Administrators
Posted
It looks like it is failing to handle timezone offsets correctly, in all honesty I don't use JavaScriptSerializer myself as I have found several issues with it. http://james.newtonking.com/json is a far better library for handling this kind of thing (even Micorsoft use it in preference to their own code in the MVC framework). In fact you can install it directly with Nuget (just search for json.net) if you prefer an easy life.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted
It looks like it is failing to handle timezone offsets correctly, in all honesty I don't use JavaScriptSerializer myself as I have found several issues with it. http://james.newtonking.com/json is a far better library for handling this kind of thing (even Micorsoft use it in preference to their own code in the MVC framework). In fact you can install it directly with Nuget (just search for json.net) if you prefer an easy life.

 

thanks :)

slow down when you need to hurry, stop when you need to move on,

look back when you need to forget, or you might slip and leave sanity

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...