JavaScript Date getUTCMilliseconds()
Syntax & Examples


Date.getUTCMilliseconds() method

The getUTCMilliseconds() method of the Date object in JavaScript returns the milliseconds (0 – 999) in the specified date according to universal time.


Syntax of Date.getUTCMilliseconds()

The syntax of Date.getUTCMilliseconds() method is:

getUTCMilliseconds()

This getUTCMilliseconds() method of Date returns the milliseconds (0 – 999) in the specified date according to universal time.

Return Type

Date.getUTCMilliseconds() returns value of type Number.



✐ Examples

1 Using getUTCMilliseconds() to get the UTC milliseconds of a specific date

In JavaScript, we can use the getUTCMilliseconds() method to get the milliseconds from a Date object according to universal time.

For example,

  1. We create a new Date object date representing January 15, 2021, at 15:30:00.500 UTC.
  2. We use the getUTCMilliseconds() method to get the milliseconds from date.
  3. The milliseconds are stored in the variable utcMilliseconds.
  4. We log utcMilliseconds to the console using console.log() method.

JavaScript Program

const date = new Date('2021-01-15T15:30:00.500Z');
const utcMilliseconds = date.getUTCMilliseconds();
console.log(utcMilliseconds);

Output

500

2 Using getUTCMilliseconds() with the current date

In JavaScript, we can use the getUTCMilliseconds() method to get the current milliseconds from the current date according to universal time.

For example,

  1. We create a new Date object now representing the current date and time.
  2. We use the getUTCMilliseconds() method to get the current milliseconds from now.
  3. The current milliseconds are stored in the variable currentUTCMilliseconds.
  4. We log currentUTCMilliseconds to the console using console.log() method.

JavaScript Program

const now = new Date();
const currentUTCMilliseconds = now.getUTCMilliseconds();
console.log(currentUTCMilliseconds);

Output

123

3 Using getUTCMilliseconds() with a specific date in the past

In JavaScript, we can use the getUTCMilliseconds() method to get the milliseconds from a specific date in the past according to universal time.

For example,

  1. We create a new Date object pastDate representing July 4, 1776, at 12:00:00.750 UTC.
  2. We use the getUTCMilliseconds() method to get the milliseconds from pastDate.
  3. The milliseconds are stored in the variable pastUTCMilliseconds.
  4. We log pastUTCMilliseconds to the console using console.log() method.

JavaScript Program

const pastDate = new Date('1776-07-04T12:00:00.750Z');
const pastUTCMilliseconds = pastDate.getUTCMilliseconds();
console.log(pastUTCMilliseconds);

Output

750

Summary

In this JavaScript tutorial, we learned about getUTCMilliseconds() method of Date: the syntax and few working examples with output and detailed explanation for each example.