Use the gettime method of Javascript to obtain the time

Source: Internet
Author: User

Javascript gettime method definition and usage

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 gettimer 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.

The following code shows the constant privatelonglLeft = 621355968000000000;

 
 
  1. Console.WriteLine(DateTime.Parse("1970-1-1").Ticks); 

For example, I want to get the number of milliseconds for "2009-7-1508: 33: 19 ".
First convert to UTC time

 
 
  1. DateTimedt1=Convert.ToDateTime("2009-7-1508:33:19").ToUniversalTime(); 

Then we can get the number of hundred seconds from January 1, January 1, 1970 to that time.
LongSticks = (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 is the Javascript gettime code.

 
 
  1. PrivatelonglLeft=621355968000000000;
  2. // Convert the number to time
  3. PublicstringGetTimeFromInt (longltime)
  4. {
  5. LongEticks= (Long) (ltime * 10000000) + lLeft;
  6. DateTimedt=NewDateTime(Eticks). ToLocalTime ();
  7. Returndt. ToString ();
  8. }
  9. // Convert the time to a number
  10. PubliclongGetIntFromTime (DateTimedt)
  11. {
  12. DateTimedt1= Dt. ToUniversalTime ();
  13. LongSticks= (Dt1.Ticks-lLeft)/10000000;
  14. ReturnSticks;
  15. }
  1. JavaScript class and inheritance: prototype attribute
  2. JavaScript class and inheritance: this property
  3. Summary of three + 1 implementation methods of ExtJS Grid Tooltip
  4. Implementation of JavaScript asynchronous call framework chain
  5. JQuery-style chained call of JavaScript asynchronous call framework

Related Article

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.