C # time stamp and JSON timestamp conversion

Source: Internet
Author: User
Tags json timestamp

Yesterday, when learning to ASP, the video mentions a problem, that is, the JSON time and C # time conversion. This conversion is required because the C # start time starts from 1 January 1 0:00:00, while JS is starting from January 1, 1970 0:00:00, which results in differences in the time conversion between the two languages. Plus the conversion of datetime-type data to JSON with "/date (...)" Such a mark, so the conversion of the data became inevitable.

There are only two ways to convert, one in the background through C # into a numeric string and then request to the browser for further parsing. The other is to send all the data directly to the browser through the JS processing to obtain.

First, C # preprocessing

after checking the information on the Internet, I found that the minimum time unit for C # is tick=100ns, while JS is 1ms, the difference is 10,000 times times. Need attention when converting. When the time stamp returned by JS is passed to C # without time zone, it is necessary to be aware of localization when JS translates into C # time. Here's the code:
 private static Int64 dt = new DateTime (1970, 1, 1).        Ticks; <summary>/////Pass in a datetime, convert it to a number of Int64 from January 1, 1970///</summary>//<param Nam E= "Time" > times required to convert </param>//<returns>int64 digital time </returns> public static Int64 csharp2j Son (DateTime time) {return Convert.toint64 (time.        TICKS-DT)/10000); }///<summary>//Convert Js/java time to C # time///</summary>//<param name= "Jstime" &G T;js/java time </param>///<example> "1335258540000" </example>//<returns></returns  > public static DateTime json2csharp (String jstime) {Int64 longtime = Convert.toint64 (jstime)           * 10000; return new DateTime (LONGTIME+=DT).        ToLocalTime (); }    

The above code already contains C # to JS and JS to C # two parts. On the front end, I can convert C # timestamps to specific times with just the code below:

var date=new date (Csharptimetick);

Second, directly through the JS processing

After the datetime of C # is converted to JSON, the default format is this: "/date (1294499956278+0800)/", to convert the time of this format, I just retrieve the middle "1294499956278" and then call the date () directly function transformation can be.

<script type= "Text/javascriopt>        var patt=/\d{13}/;        var timenum=paresint (Tick.match (Patt));//tick is the JSON timestamp obtained from C #. Here is the string to be converted into a number.        var d=new Date (Timenum);</script>

  

C # time stamp and JSON timestamp conversion

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.