Timestamp conversion between js and c-sharp

  • 2020-03-30 04:19:03
  • OfStack

JS timestamp to C# time, and then C# timestamp to JS time

Timestamp in JS


 var dt = new Date().getTime();//Time stamp

C# timestamp turns time


            DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime( new DateTime(1970, 1, 1));
            long lTime = long.Parse(dt + "0000");  //Under the explanation, the time format is 13 bits followed by 4 "0", if the time format is 10 bits followed by 7 "0", as for why I am not quite clear, is also the code conversion after someone else wrote
            TimeSpan toNow = new TimeSpan(lTime);
            DateTime dtResult = dtStart.Add(toNow); //Get the converted time

-------------------------------------------------------------------------------
C# time to timestamp


            System. DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime( new System. DateTime(1970, 1, 1, 0, 0, 0, 0));
            //intResult = (time- startTime).TotalMilliseconds;
DateTime dtResult//Getting time & NBSP;               < br / > long t = (dtResult.Ticks - startTime.Ticks) / 10000;//Divide 10000 to 13 bits

JS


       var d = new Date(data);  //Time stamp rotation time
       alert(formatDate(d));
 //Format time
 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+ "-"+date+ "   "+hour+":" +minute+":" +second;   
    } 

The code is super simple, but it's all very useful


Related articles: