The code sharing of C # timestamp and JS timestamp Mutual transfer method is introduced in detail.

Source: Internet
Author: User
The following small series for everyone to bring a JS timestamp and C # Timestamp Mutual transfer method (recommended). Small series feel very good, now share to everyone, also for everyone to make a reference. Let's take a look at it with a little knitting.

Examples are as follows:

Using system;using system.collections.generic;using system.linq;using system.web;using System.Web.UI;using system.web.ui.webcontrols;using newtonsoft.json;namespace testweb{public partial class Ajax:System.Web.UI.Page {p rotected void Page_Load (object sender, EventArgs e) {if (!      IsPostBack) {//testajax ()}} public void Testajax () {var phone = request.form["Phone"];      var authcode = request.form["Authcode"]; var pt = request.form["PT"]; JS timestamp new Date (). GetTime () eg:1429503106452 string outputmsg = string.      Empty; if (phone! = NULL && Authcode! = null && pt! = null) {DateTime Dtstart = Timezone.currenttime        Zone.tolocaltime (New DateTime (1970, 1, 1)); Note, the time format is 13-bit followed by the addition of 4 "0", if the time format is 10 bits, then add 7 "0" long ltime = long. Parse (PT + (Pt. Length = = 13?        "0000": "0000000"));        TimeSpan Tonow = new TimeSpan (Ltime); DateTime Dtresult = Dtstart.add (Tonow); Gets the converted time stringstr = dtresult.tostring ();      Outputmsg = outmsg (new Responseinfo {success = true, Tag = +, msg = "Success"});    } Response.Write (Outputmsg); } public Long Getcurrentticksforjs () {System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime (New Sys Tem.      DateTime (1970, 1, 1, 0, 0, 0, 0));      DateTime Dtresult = datetime.now;//get time long T = (dtresult.ticks-starttime.ticks)/10000;//except 10000 adjust to 13 bit    return t; The public string outmsg (object obj) {return jsonconvert.serializeobject (obj, Newtonsoft.Json.Formatting.Indente    D);      public class Responseinfo {public bool success {get; set;}      public int tag {get; set;}    public string msg {get; set;} }    //...  }} <%@ page language= "C #" autoeventwireup= "true" codebehind= "Ajax.aspx.cs" inherits= "Testweb.ajax"%><script  Type= "Text/javascript" > var d = new Date (<%=getcurrentticksforjs ()%>);   Alert (FormatDate (d));   function FormatDate (now) { var year = Now.getfullyear ();    var month = Now.getmonth () + 1;    var date = Now.getdate ();    var hour = now.gethours ();    var minute = Now.getminutes ();    var second = Now.getseconds (); Return year + "-" + (month.tostring (). Length ==1? "0" +month:month) + "-" + (date.tostring (). Length ==1?  "0" +date:date + "+ Hour +": "+ Minute +": "+ second; }</script>var date = new Date (1459481266695); Y = date.getfullyear () + '-'; M = (Date.getmonth () +1 < 10? ' 0 ' + (date.getmonth () +1): Date.getmonth () +1) + '-';D = date.getdate () + '; h = date.gethours () + ': '; m = date.getminutes ( ) + ': '; s = date.getseconds (); Console.log (Y+m+d+h+m+s); Vm307:9 2016-04-1 11:27:46
Related Article

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.