Convert date. gettime In JScript to datetime in. net

Source: Internet
Author: User
Tags net time

Gettime in JS is similar to datetime. ticks in. net, so the two concepts need to be used for mutual conversion. However, gettime and ticks have different meanings. See the following definitions:

Gettime definition in 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 relationships between them are as follows:

1 second = 10 ^ 3 ms
1 millisecond = 10 ^ 3 microseconds
1 microsecond = 10 ^ 3 microseconds

After viewing the definition of gettime and ticks, we can know that gettime is in milliseconds, and ticks is in 100 nanoseconds. Therefore, the difference between gettime and ticks is 1000*1000/100. gettime () since-1-1 and-1, ticks starts from-1-1. Therefore, ticks has an additional time before-1. The conversion formula is as follows:

Gettime () * 1000*1000/100 + datetime. parse ("1970-1-1"). ticks

The test procedure is as follows:

// JS time to. NET Time
Long maid = 1247617999000;
Console. writeline (jsgettime );
Long jsbegintick = datetime. parse ("1970-1-1"). ticks;

Long netticks = jsgettime * 1000*10 + jsbegintick;
Datetime dt = new datetime (netticks );
Console. writeline (Dt. tostring ("yyyy-mm-dd hh: mm: SS "));
Datetime dt2 = DT. tolocaltime ();
Console. writeline (dt2.tostring ("yyyy-mm-dd hh: mm: SS "));

// Convert. NET Time to JS gettime ();
Long dt2ticks = dt2.touniversaltime (). ticks;
Long dt2jsticks = (dt2ticks-jsbegintick)/(1000*10 );
Console. writeline (dt2jsticks );
Console. Read ();

From:

Http://www.cnblogs.com/jordan2009/archive/2009/08/17/1547875.html

 

 

Private long getjstimes ()
{
Datetime dt = datetime. now;
Long dtticks = DT. touniversaltime (). ticks;
Long jsbegintick = datetime. parse ("1970-1-1"). ticks;
Long dtjsticks = (dtticks-jsbegintick)/(1000*10 );
Return dtjsticks;
}

Private datetime todotnettime (long jstime)
{
Long jsbegintick = datetime. parse ("1970-1-1"). ticks;

Long netticks = jstime * 1000*10 + jsbegintick;
Datetime dt = new datetime (netticks );
Datetime dt2 = DT. tolocaltime ();

Return dt2;

}

 

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.