JavaScript Date setMilliseconds()

Posted by

Method

JavaScript Date setMilliseconds() Method

The setMilliseconds() method of the JavaScript Date object sets the milliseconds of a date object to a specified value.

Syntax

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

dateObj.setMilliseconds(millisecondsValue)

[/dm_code_snippet]

Where,

  • dateObj is a valid Date object.
  • millisecondsValue is an integer value between 0 and 999, representing the milliseconds.

Return Value

The setMilliseconds() method returns the number of milliseconds that have elapsed since 1 January 1970 00:00:00 UTC.

Example

The following example shows the usage of setMilliseconds() method.

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

<script type="text/javascript">
   var d = new Date();
   document.write("Initial milliseconds : " + d.getMilliseconds());
   d.setMilliseconds(888);
   document.write("<br>Updated milliseconds : " + d.getMilliseconds());
</script>

[/dm_code_snippet]

Output

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

Initial milliseconds : 841
Updated milliseconds : 888

[/dm_code_snippet]