Gettime definition and usage of JS
The gettime () method returns the number of milliseconds from January 1, January 1, 1970.
Ticks in C #
The value of this attribute indicates the number of intervals between January 1, 0001 nanoseconds that have elapsed since midnight, January 1, 100.
For a few words, the nanoseconds are also called microseconds. The Conversion Relationship between them is
1 second = 10 ^ 3 ms
1 millisecond = 10 ^ 3 microseconds
1 microsecond = 10 ^ 3 nanoseconds
Now we need to use C # To make this JS gettime effect. First, we need to convert it into a unified unit, and we will convert it into a hundred S.
The next step is to achieve a unified time point, starting from January 1, January 1, 1970. Then, we need to calculate how many nanoseconds have elapsed since January 1, January 1-19, 0001 to January 1, January 1.
You can use the followingCodeWe know that a constant private long lleft = 621355968000000000;
Console. writeline (datetime. parse ("1970-1-1"). ticks );
For example, we need to get the number of milliseconds for "08:33:19 ".
First convert to UTC time
Datetime dt1 = convert. todatetime ("08:33:19"). touniversaltime ();
Then we can get the number of hundred seconds from January 1, January 1, 1970 to that time.
Long sticks = (dt1.ticks-datetime. parse ("1970-1-1"). ticks)
The final result is to convert the result to JS, so the second is converted to millisecond, sticks/10000000; the final result is 1247617999.
Similarly, if we convert the number of milliseconds to the local time, we will return it. The difference between Beijing and UTC is 8 hours. Therefore, we can use tolocaltime to convert it to the local time to solve the time difference problem.
The following code is used:
Private long lleft = 621355968000000000;
// Convert the number to time
Public String gettimefromint (long ltime)
{
Long eticks = (long) (ltime * 10000000) + lleft;
Datetime dt = new datetime (eticks). tolocaltime ();
Return DT. tostring ();
}
// Convert the time to a number
Public long getintfromtime (datetime DT)
{
Datetime dt1 = DT. touniversaltime ();
Long sticks = (dt1.ticks-lleft)/10000000;
Return sticks;
}