How to get milliseconds in javascript

PHPz
Release: 2023-05-21 09:27:06
Original
5488 people have browsed it

As a programming language that interacts most closely with users, JavaScript often needs to obtain the accuracy of time. The method of obtaining milliseconds is very common in JavaScript. This article will introduce the method of obtaining milliseconds in JavaScript.

1. Date object

The Date object in JavaScript is used to represent time and date. Use the Date object to get the milliseconds of the current time. In JavaScript, the timestamp is calculated from January 1, 1970, and the timestamp stored in the computer is the number of milliseconds from 0:00:00 on January 1, 1970.

For example:

var currentDate = new Date();
var milliSeconds = currentDate.getTime();
Copy after login

In this example, we create a new Date object and use the getTime() function to obtain the number of milliseconds represented by the timestamp represented by the object.

2. Performance object

The performance API is an interface provided by the browser for measuring page performance and execution time. In the performance API, we can use the now() method to get the number of milliseconds of the current time.

For example:

var timeInMilliseconds = window.performance.now();
Copy after login

In this example, we use the now() function to access the performance object to get the milliseconds of the current time.

It should be noted that the value returned by the now() method is a number of microseconds, which needs to be divided by 1000 to get the number of milliseconds.

3. Use the Date.now() function

The Date.now() function is a new function introduced in ES5 and is used to obtain the milliseconds of the current time. This function is simpler, faster, and more accurate than the getTime() function and performance.now() function using Date objects.

For example:

var timeInMilliseconds = Date.now();
Copy after login

In this example, we use the Date.now() function to directly obtain the number of milliseconds of the current time.

Summary:

The above three methods can be used to get the number of milliseconds in JavaScript. However, it should be noted that performance aspects need to be paid attention to. In terms of performance, the Date.now() function is the fastest, followed by the performance.now() function, and the slowest is the getTime() function of the Date object. Therefore, in actual development, we need to choose the method that is most suitable for this project.

The above is the detailed content of How to get milliseconds in javascript. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template