JavaScript Date getUTCMilliseconds() Method



The JavaScript Date.getUTCMilliseconds() method is used to retrieve the milliseconds value of a date object according to the UTC. This method returns a numeric value that represents the milliseconds (between 0 to 999) of the specified date and time in UTC. If we don't specify the milliseconds component in the date object, this method defaults to "0".

UTC stands for Coordinated Universal Time. It is the primary time standard by which the world regulates clocks and time.

Syntax

Following is the syntax of JavaScript Date getUTCMilliseconds() Method −

getUTCMilliseconds();

This method does not accept any parameters.

Return Value

A number representing the milliseconds (between 0 and 999) of the specified date and time in UTC.

Example 1

In the following example, we are demonstrating the basic usage of JavaScript Date getUTCMilliseconds() method −

<html>
<body>
<script>
   const currentDate = new Date();
   const utcmilliseconds = currentDate.getUTCMilliseconds();

   document.write(utcmilliseconds);
</script>
</body>
</html>

Output

The above program returns the milliseconds of a date according to UTC.

Example 2

In this example, we are getting the milliseconds value from the specified date value −

<html>
<body>
<script>
   const specificDate = new Date('2023-10-26 12:30:45.154');
   const utcmilliseconds = specificDate.getUTCMilliseconds();

   document.write(utcmilliseconds);
</script>
</body>
</html>

Output

The above programs returns integer 154 as milliseconds value.

Example 3

If we doesn't specify the milliseconds component in the provided date, it will return "0" as default value −

<html>
<body>
<script>
   const specificDate = new Date('2023-10-26 12:30:45');
   const utcmilliseconds = specificDate.getUTCMilliseconds();

   document.write(utcmilliseconds);
</script>
</body>
</html>

Output

As we can see the output, "0" is returned.

Example 4

If the Date object is invalid, this mehtod will return "NaN" as result −

<html>
<body>
<script>
   const specificDate = new Date('sagtrjdh');
   const utcmilliseconds = specificDate.getUTCMilliseconds();

   document.write(utcmilliseconds);
</script>
</body>
</html>

Output

As we can see the output, "NaN" is returned.

Advertisements