JavaScript getMilliseconds(): Get the milliseconds (0-999)

The JavaScript getMilliseconds() method is used to get the milliseconds (0-999). For example:

HTML with JavaScript Code
<!DOCTYPE html>
<html>
<body>
   
   <p id="xyz"></p>

   <script>
      const d = new Date();
      let ms = d.getMilliseconds();
      document.getElementById("xyz").innerHTML = ms;
   </script>

</body>
</html>
Output

In the above example, the following JavaScript statement:

const d = new Date();
creates a new "Date" object with the "new Date()" constructor. This method generates a new date and time object using the current system clock time.

Then the following JavaScript statement:

let ms = d.getMilliseconds();

calls the Date object's getMilliseconds() method to get the current milliseconds (0-999). The ms variable stores this value.

Finally, the following JavaScript statement:

document.getElementById("xyz").innerHTML = ms;

finds the HTML element with the id attribute "xyz" and sets its innerHTML property to the value stored in the ms variable using the document.getElementById() method. This will display the current millisecond count on the web page.

JavaScript getMilliseconds() syntax

The syntax of the getMilliseconds() method in JavaScript is:

x.getMilliseconds()

where x must be an object of the Date() constructor.

The getMilliseconds() method returns a number from 0 to 999, which will be the milliseconds of the local time.

Please note: To display the date in the format dd-mm-yyyy, refer to its separate example.

Please note: To display time in the format hh:mm:ss, refer to its separate example.

Please note: To display time in the format hh:mm:ss AM/PM, refer to its separate example.

Advantages of the getMilliseconds() method in JavaScript

Disadvantages of the getMilliseconds() method in JavaScript

JavaScript Online Test


« Previous Tutorial Next Tutorial »


Liked this post? Share it!