JavaScript getUTCMilliseconds(): Get the UTC Milliseconds

The JavaScript getUTCMilliseconds() method is used to get the milliseconds (0-999) according to Universal Time Coordinated (UTC) or Greenwich Mean Time (GMT). For example:

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

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

</body>
</html>
Output

In the above example, the following JavaScript code:

const d = new Date();

creates a new "Date" object, which represents the current date and time. Then the following JavaScript statement:

let ms = d.getUTCMilliseconds();

calls the getUTCMilliseconds() method on the Date object, which returns the number of milliseconds since the last full second in the UTC timezone. And then store the result of getUTCMilliseconds() in a variable called ms.

Finally, the following, or the third JavaScript statement:

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

gets the HTML element with the ID "xyz" using the getElementById() method, which then sets the innerHTML property of the HTML element to the value of ms.

JavaScript getUTCMilliseconds() syntax

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

x.getUTCMilliseconds()

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

Please note: The getUTCMilliseconds() method returns a number from 0 to 999, which will be the milliseconds of the UTC time.

Advantages of getUTCMilliseconds() in JavaScript

Disadvantages of getUTCMilliseconds() in JavaScript

JavaScript Online Test


« Previous Tutorial Next Tutorial »


Liked this post? Share it!