• JavaScript Video Tutorials

JavaScript Date setUTCMilliseconds() Method



In JavaScript, the Date.setUTCMilliseconds() method is used to set the "milliseconds" of a Date object according to the UTC (Coordinated Universal Time) time zone. It allows you to modify the milliseconds component of a Date object without changing the other parts of the date and time. This method returns the milliseconds component of the updated Date object after the modification.

UTC stands for Coordinated Universal Time. It is the primary time standard by which the world regulates clocks and time. The time difference between IST (Indian standard time) and UTC is as UTC+5:30 (i.e. 5 hours 30 minutes).

Syntax

Following is the syntax of JavaScript Date setUTCilliseconds() method −

setUTCMilliseconds(millisecondsValue);

Parameters

This method accepts only one parameter. The same is described below −

  • millisecondsValue An integer representing the milliseconds (0 to 999).
    • If -1 is provided, it will result in the last millisecond of the previous second.
    • If 1000 is provided, it will result in the first millisecond of the next second.

Return Value

The return value of setUTCMilliseconds() is the updated milliseconds component of the Date object, represented as the number of milliseconds since the Unix epoch (January 1, 1970, 00:00:00 UTC).

Example 1

In the following example, we are using the JavaScript Date setUTCMilliseconds() method to set the "milliseconds" to 500, according to UTC time −

<html>
<body>
<script>
   const myDate = new Date();
   myDate.setUTCMilliseconds(500);

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

Output

If we execute the above program, the milliseconds will be set to 500.

Example 2

If we provide "-1" for millisecondsValue, this method will gives the last millisecond of the previous second −

<html>
<body>
<script>
   const myDate = new Date();
   myDate.setUTCMilliseconds(-1);

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

Output

This method returns "999" as last millisecond of the previous second.

Example 3

If we provide "1000" for millisecondsValue, this method will gives the first millisecond of the next second −

<html>
<body>
<script>
   const myDate = new Date();
   myDate.setUTCMilliseconds(1000);

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

Output

This method returns "0" as first millisecond of the next second.

Advertisements