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,
- We create a new Date object
date
representing January 15, 2021, at 15:30:00.500 UTC. - We use the
getUTCMilliseconds()
method to get the milliseconds fromdate
. - The milliseconds are stored in the variable
utcMilliseconds
. - We log
utcMilliseconds
to the console usingconsole.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,
- We create a new Date object
now
representing the current date and time. - We use the
getUTCMilliseconds()
method to get the current milliseconds fromnow
. - The current milliseconds are stored in the variable
currentUTCMilliseconds
. - We log
currentUTCMilliseconds
to the console usingconsole.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,
- We create a new Date object
pastDate
representing July 4, 1776, at 12:00:00.750 UTC. - We use the
getUTCMilliseconds()
method to get the milliseconds frompastDate
. - The milliseconds are stored in the variable
pastUTCMilliseconds
. - We log
pastUTCMilliseconds
to the console usingconsole.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.