Json date format converted to datetime

Source: Internet
Author: User

JSON Date form: "/date (1242357713797+0800)/", below we will use the following C # method to convert him to a datetime type:


<summary>
The Json date format is converted to a. Net datetime type
</summary>
<param name= "Jsondate" >json date, for example:/date (928120800000+0800)/</param>
<returns></returns>
public static DateTime Jsontodatetime (String jsondate)
{
String value = jsondate.substring (6, jsondate.length-8);
DateTimeKind kind = DATETIMEKIND.UTC;
int index = value. IndexOf (' + ', 1);
if (index = =-1)
index = value. IndexOf ('-', 1);
if (Index! =-1)
{
kind = datetimekind.local;
Value = value. Substring (0, index);
}
Long javascriptticks = long. Parse (value, System.Globalization.NumberStyles.Integer, System.Globalization.CultureInfo.InvariantCulture);
Long initialjavascriptdateticks = (New DateTime (1970, 1, 1, 0, 0, 0, DATETIMEKIND.UTC)). Ticks;
DateTime utcdatetime = new DateTime ((Javascriptticks * 10000) + initialjavascriptdateticks, DATETIMEKIND.UTC);
datetime datetime;
Switch (kind)
{
Case Datetimekind.unspecified:
DateTime = Datetime.specifykind (Utcdatetime.tolocaltime (), datetimekind.unspecified);
Break
Case Datetimekind.local:
DateTime = Utcdatetime.tolocaltime ();
Break
Default
DateTime = UtcDateTime;
Break
}
return dateTime;
}

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.