none
DateTime Datamember Serialize & Deserialize? RRS feed

  • Question

  • Hi Team,

        [DataContract]
        public class  Details
        {

          string empName = string.Empty; 

          DateTime? startDate;

           [DataMember]       
           public string EmployeeName { get { return empName; } set { empName = value; } }

          [DataMember]       
           public DateTime? StartDateTime { get { return startDate; } set { startDate = value; } }

         }

        [DataContract]
        public class  AllDetails
        {

         string empName = string.Empty; 

         DateTime? startDate;

          [DataMember]       
           public string EmployeeName { get { return empName; } set { empName = value; } }

          [DataMember]       
           public DateTime? StartDateTime { get { return startDate; } set { startDate = value; } }

    }

      public CustomMessage UpdateDetails(Details  details)
            {  
              string detailsObj = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(details);
              AllDetails  allDetails = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<AllDetails>(detailsObj );

            }

    The  StartDateTime value passed  from my WCF Test Client is  9/2/2015  5:45:00 PM.

    Before to Serialize  the Details Object, the StartDateTime value  shows  the same which is passed from WCF Test Client i.e  9/2/2015  5:45:00 PM.

    But After DeSerialize  the serialized Details Object, the StartDateTime value has changed  to  different value i.e  9/2/2015  9:45:00 PM. 

    The issue is with StartDateTime value. After DeSerialize the StartDateTime value has changed. Need inputs to  solve this issue and show the same StartDateTime value  which is initially passed. please correct me and do the needful.

    Thanks

    Vamshi


    Vamshi Janagama

    Thursday, September 3, 2015 3:16 AM

Answers

  • Hi Vamshi Janagama,

    According to this issue, as far as I know, when we use javascriptSerializer to  serialize  time,

    it will serialize into UTC time, and deserialization did not back to local time, difference between

    local time and UTC time is 8 hours, this leads to this issue, so, after deserialization we need to call the

    ToLocalTime method converts DateTime to the local time.

    For example:

    JavascriptSerializer serializer = new JavascriptSerializer();
    DateTime now = DateTime.Parse("2015-09-03 00:00:00");
    Console.WriteLine(serializer.Deserialize<DateTime>(serializer.Serialize(now)).ToLocalTime());

    If we not use TolocalTime method, then it will display the time as "2015-09-02 16:00:00".

    I hope that will be helpful to you.

    Best Regards,

    Grady

    Thursday, September 3, 2015 12:37 PM
    Moderator

All replies

  • Hi Vamshi Janagama,

    According to this issue, as far as I know, when we use javascriptSerializer to  serialize  time,

    it will serialize into UTC time, and deserialization did not back to local time, difference between

    local time and UTC time is 8 hours, this leads to this issue, so, after deserialization we need to call the

    ToLocalTime method converts DateTime to the local time.

    For example:

    JavascriptSerializer serializer = new JavascriptSerializer();
    DateTime now = DateTime.Parse("2015-09-03 00:00:00");
    Console.WriteLine(serializer.Deserialize<DateTime>(serializer.Serialize(now)).ToLocalTime());

    If we not use TolocalTime method, then it will display the time as "2015-09-02 16:00:00".

    I hope that will be helpful to you.

    Best Regards,

    Grady

    Thursday, September 3, 2015 12:37 PM
    Moderator
  • Grady,

    Thank You for the response.

    In the below contract,  StartDateTime value is Deserialized and is available in AllDetails  object. how will i able to retrieve the StartDateTime value from the AllDetails  object and use TolocalTime method to convert the time in StartDateTime?

    public CustomMessage UpdateDetails(Details  details)

            {  
              string detailsObj = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(details);
              AllDetails  allDetails = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<AllDetails>(detailsObj );

    Thanks

    Vamshi


    Vamshi Janagama

    Friday, September 18, 2015 3:33 PM
  • Did you find an answer for this issue? I'm getting the same issue, how to get the date if is part of a complex object or list of objects.
    Wednesday, July 18, 2018 6:22 PM