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 conversion. net time long jsGetTime = 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 "));//. net Time to JS getTime (); long dt2Ticks = dt2.ToUniversalTime (). ticks; long dt2JsTicks = (dt2Ticks-jsBeginTick)/(1000*10); Console. writeLine (dt2JsTicks); Console. read ();

 

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.