SoFunction
Updated on 2025-03-07

Timestamp conversion between js and C#

Convert JS timestamp to time in C#, and then convert the timestamp in C# to time in JS

Timestamp in JS

Copy the codeThe code is as follows:

var dt = new Date().getTime();// Timestamp

C# Timestamp to time

Copy the codeThe code is as follows:

            DateTime dtStart = ( new DateTime(1970, 1, 1));
long lTime = (dt + "0000"); //Instructions, the time format is 13 bits and add 4 "0" to the next. If the time format is 10 bits, add 7 "0" to the next. As for why I don't know much, it is also converted according to the code written by others.
            TimeSpan toNow = new TimeSpan(lTime);
DateTime dtResult = (toNow); //Get the converted time

-------------------------------------------------------------------------------
C# Time to Time Stamp

Copy the codeThe code is as follows:

            System. DateTime startTime = ( new System. DateTime(1970, 1, 1, 0, 0, 0, 0));
            //intResult = (time- startTime).TotalMilliseconds;
DateTime dtResult//Get time
long t = ( - ) / 10000;//Adjust 10000 to 13 digits

JS

Copy the codeThe code is as follows:

var d = new Date(data); //Timestamp to time
       alert(formatDate(d));
//Format time
 function    formatDate(now)   {   
        var   year=();   
        var   month=()+1;   
        var   date=();   
        var   hour=();   
        var   minute=();   
        var   second=();   
        return   year+ "-"+month+ "-"+date+ "   "+hour+":" +minute+":" +second;   
    } 

The code is super simple, but it is very practical. Please refer to it.