JavaScript Date setUTCMilliseconds()
Syntax & Examples
Date.setUTCMilliseconds() method
The setUTCMilliseconds() method of the Date object in JavaScript sets the milliseconds for a specified date according to universal time (UTC). This method updates the Date object to the specified milliseconds.
Syntax of Date.setUTCMilliseconds()
The syntax of Date.setUTCMilliseconds() method is:
setUTCMilliseconds(millisecondsValue)
This setUTCMilliseconds() method of Date sets the milliseconds for a specified date according to universal time.
Parameters
Parameter | Optional/Required | Description |
---|---|---|
millisecondsValue | required | A number representing the milliseconds to be set, ranging from 0 to 999. |
Return Type
Date.setUTCMilliseconds() returns value of type Number
.
✐ Examples
1 Using setUTCMilliseconds() to set the milliseconds
In JavaScript, we can use the setUTCMilliseconds()
method to set the milliseconds of a Date object to a specific value.
For example,
- We create a new Date object
date
representing January 1, 2000. - We use the
setUTCMilliseconds()
method to set the milliseconds of thedate
object to 500. - The updated date is stored in the variable
newDate
. - We log
newDate
to the console usingconsole.log()
method.
JavaScript Program
const date = new Date('2000-01-01T00:00:00Z');
date.setUTCMilliseconds(500);
console.log(date);
Output
Sat Jan 01 2000 00:00:00 GMT+0000 (Coordinated Universal Time)
2 Using setUTCMilliseconds() with a specific date
We can use the setUTCMilliseconds()
method to set the milliseconds of a specific Date object.
For example,
- We create a new Date object
date
representing July 4, 1776. - We use the
setUTCMilliseconds()
method to set the milliseconds of thedate
object to 750. - The updated date is stored in the variable
newDate
. - We log
newDate
to the console usingconsole.log()
method.
JavaScript Program
const date = new Date('1776-07-04T00:00:00Z');
date.setUTCMilliseconds(750);
console.log(date);
Output
Thu Jul 04 1776 00:00:00 GMT+0000 (Coordinated Universal Time)
3 Using setUTCMilliseconds() with the current date
We can use the setUTCMilliseconds()
method to set the milliseconds of the current date.
For example,
- We create a new Date object
date
representing the current date and time. - We use the
setUTCMilliseconds()
method to set the milliseconds of thedate
object to 123. - The updated date is stored in the variable
newDate
. - We log
newDate
to the console usingconsole.log()
method.
JavaScript Program
const date = new Date();
date.setUTCMilliseconds(123);
console.log(date);
Output
Depends on the current date and time (e.g., 'Mon May 27 2024 12:34:56 GMT+0000 (Coordinated Universal Time)')
Summary
In this JavaScript tutorial, we learned about setUTCMilliseconds() method of Date: the syntax and few working examples with output and detailed explanation for each example.