Notes for using javascriptserializer to serialize date types

Source: Internet
Author: User

Javascriptserializer indicates the scale when serializing datetime. In JSON, the scale is represented as: \/date (315547200000) \/a closure is made with V, and the scale of date is enclosed in brackets, the scale is the number of milliseconds after which the time tick is answered. the end time of the scale calculation is UTC, which starts at midnight on January 1, January 1, 1970,
Note that the start time and end time are UTC time.Program:
Customer cs = new customer ();
CS. Birthday = new datetime (1980, 1, 1, 12, 0, 0 );
CS. Name = "Steven ";
CS. Sex = true;

Javascriptserializer JS = new javascriptserializer ();
String output = Js. serialize (CS );

Customer Cust = Js. deserialize (output, typeof (customer) as customer;
If (Cust! = NULL)
{

Txtbirthday. Text = Cust. Birthday. value. tostring ();

}
In this case, the date after serialization is not 12:00:00, but a time obtained from UTC time, which may be 4:00:00, in this way, the UI display will bring consistency issues!
Other processing is very simple. Before serialization, convert the local time to UTC time, and then convert the UTC time to the local time after deserialization. Code :
Customer cs = new customer ();
CS. Birthday = new datetime (1980, 1, 1, 12, 0, 0). touniversaltime ();
CS. Name = "Steven ";
CS. Sex = true;

javascriptserializer JS = new javascriptserializer ();
string output = Js. serialize (CS);
customer Cust = Js. deserialize (output, typeof (customer) as customer;
If (Cust! = NULL)
{< br>
txtbirthday. TEXT = Cust. birthday. value. tolocaltime (). tostring ();

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.