JavaScript Date getMilliseconds()
Syntax & Examples
Date.getMilliseconds() method
The getMilliseconds() method of the Date object in JavaScript returns the milliseconds (0 – 999) in the specified date according to local time.
Syntax of Date.getMilliseconds()
The syntax of Date.getMilliseconds() method is:
getMilliseconds()This getMilliseconds() method of Date returns the milliseconds (0 – 999) in the specified date according to local time.
Return Type
Date.getMilliseconds() returns value of type Number.
✐ Examples
1 Using getMilliseconds() to get the milliseconds of a specific date
In JavaScript, we can use the getMilliseconds() method to get the milliseconds from a Date object.
For example,
- We create a new Date object
daterepresenting January 15, 2021, at 15:30:00.500. - We use the
getMilliseconds()method to get the milliseconds fromdate. - The milliseconds are stored in the variable
milliseconds. - We log
millisecondsto the console usingconsole.log()method.
JavaScript Program
const date = new Date('2021-01-15T15:30:00.500');
const milliseconds = date.getMilliseconds();
console.log(milliseconds);Output
500
2 Using getMilliseconds() with the current date
In JavaScript, we can use the getMilliseconds() method to get the current milliseconds from the current date.
For example,
- We create a new Date object
nowrepresenting the current date and time. - We use the
getMilliseconds()method to get the current milliseconds fromnow. - The current milliseconds are stored in the variable
currentMilliseconds. - We log
currentMillisecondsto the console usingconsole.log()method.
JavaScript Program
const now = new Date();
const currentMilliseconds = now.getMilliseconds();
console.log(currentMilliseconds);Output
123
3 Using getMilliseconds() with a specific date in the past
In JavaScript, we can use the getMilliseconds() method to get the milliseconds from a specific date in the past.
For example,
- We create a new Date object
pastDaterepresenting July 4, 1776, at 12:00:00.750. - We use the
getMilliseconds()method to get the milliseconds frompastDate. - The milliseconds are stored in the variable
pastMilliseconds. - We log
pastMillisecondsto the console usingconsole.log()method.
JavaScript Program
const pastDate = new Date('1776-07-04T12:00:00.750');
const pastMilliseconds = pastDate.getMilliseconds();
console.log(pastMilliseconds);Output
750
Summary
In this JavaScript tutorial, we learned about getMilliseconds() method of Date: the syntax and few working examples with output and detailed explanation for each example.