The setMilliseconds method in JavaScript is utilized to assign the milliseconds component for a given date, relying on the local time settings.
Syntax
The syntax for the setMilliseconds function is depicted as follows:
dateObj.setMilliseconds( msValue)
Parameter
msValue - This signifies an integer ranging from 0 to 999, denoting the millisecond value. In cases where the supplied millisecond exceeds 999, the setMilliseconds function adjusts the second value accordingly.
JavaScript Date setMilliseconds method example
In this section, we will explore the setMilliseconds method by examining several examples.
Example 1
Let us examine an example that demonstrates how to display both the current and the modified value of seconds.
<script>
var ms=new Date();
document.writeln("Current millisecond : "+ms.getMilliseconds()+"<br>");
ms.setMilliseconds(825);
document.writeln("Updated millisecond : "+ms.getMilliseconds());
</script>
Output:
Current millisecond : 750
Updated millisecond : 825
Example 2
Let’s examine an illustration that demonstrates how to modify the second value of the specified time.
<script>
var ms=new Date("August 15, 1947 20:22:110")
ms.setMilliseconds(825);
document.writeln("Updated millisecond : "+ms.getMilliseconds());
</script>
Output:
Updated millisecond : 825