C # implement JavaScript gettime ()

Source: Internet
Author: User

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;

}

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.