.setUTCMilliseconds()
The .setUTCMilliseconds()
method sets the milliseconds value of a Date
object according to Coordinated Universal Time(UTC) and returns the updated Date
object.
Syntax
myDate.setUTCMilliseconds(millisecondsValue);
The .setUTCMilliseconds()
method takes a single parameter, representing the numeric value of the milliseconds.
Note: It’s important to note that the setUTCMilliseconds() method modifies the Date object in place. It returns the number of milliseconds since January 1, 1970, but it’s typically not necessary to use the return value unless it’s specifically needed for calculations.
Example
The JavaScript Date
object has the setUTCMilliseconds()
method to set the milliseconds component of a date object in UTC. Here’s an example that shows how to use the setUTCMilliseconds()
method to set the milliseconds of a date:
// Function to display date in a human-readable format including millisecondsfunction formatDateTime(dateTime) {const options = {year: 'numeric',month: 'numeric',day: 'numeric',hour: 'numeric',minute: 'numeric',second: 'numeric',timeZone: 'UTC',};const formattedDate = dateTime.toLocaleString('en-US', options);const milliseconds = dateTime.getUTCMilliseconds();return `${formattedDate}.${milliseconds.toString().padStart(3, '0')}`;}// Create a new Date object with specific data: July 24, 2023, 12:34:56.789 UTCconst date = new Date(Date.UTC(2023, 6, 24, 12, 34, 56, 789));// Get the current milliseconds in UTCconst currentUTCMilliseconds = date.getUTCMilliseconds();// Set a new value for milliseconds (e.g., 500 milliseconds)const newUTCMilliseconds = 500;// Create a new Date object with the same date but modified millisecondsconst newDateWithModifiedMilliseconds = new Date(date);newDateWithModifiedMilliseconds.setUTCMilliseconds(newUTCMilliseconds);console.log('Original Date:', formatDateTime(date));console.log('Current UTC Milliseconds:', currentUTCMilliseconds);console.log('Modified Date with UTC Milliseconds:',formatDateTime(newDateWithModifiedMilliseconds));
This example results in the following output:
Original Date: 7/24/2023, 12:34:56.789
Current UTC Milliseconds: 789
Modified Date with UTC Milliseconds: 7/24/2023, 12:34:56.500
Note: In this example
formatDateTime()
function manually appends the milliseconds component to the formatted date. We usegetUTCMilliseconds()
to retrieve the milliseconds value from theDate
object and then pad it with leading zeros to ensure it is displayed with three digits.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn JavaScript on Codecademy
- Career path
Front-End Engineer
Front-end engineers work closely with designers to make websites beautiful, functional, and fast.Includes 34 CoursesWith Professional CertificationBeginner Friendly115 hours - Free course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner Friendly15 hours