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