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

ParameterOptional/RequiredDescription
millisecondsValuerequiredA 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,

  1. We create a new Date object date representing January 1, 2000.
  2. We use the setUTCMilliseconds() method to set the milliseconds of the date object to 500.
  3. The updated date is stored in the variable newDate.
  4. We log newDate to the console using console.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,

  1. We create a new Date object date representing July 4, 1776.
  2. We use the setUTCMilliseconds() method to set the milliseconds of the date object to 750.
  3. The updated date is stored in the variable newDate.
  4. We log newDate to the console using console.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,

  1. We create a new Date object date representing the current date and time.
  2. We use the setUTCMilliseconds() method to set the milliseconds of the date object to 123.
  3. The updated date is stored in the variable newDate.
  4. We log newDate to the console using console.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.