.getMilliseconds()

d1payan's avatar
Published Jun 15, 2023
Contribute to Docs

The .getMilliseconds() method returns the milliseconds of a Date object according to the local time.

Syntax

myDate.getMilliseconds();

The return value of .getMilliseconds() is an integer number between 0 and 999, representing the milliseconds of a date according to the local time.

Example

The code below determines if the given value is equivalent to half a second, or not:

const today = new Date('June 10, 2023 15:50:45:500');
if (today.getMilliseconds() === 500) {
console.log('It is half a second.');
} else {
console.log('It is not half a second.');
}

This results in the following output:

It is half a second.

Codebyte Example

In the following example, currentDateTime is defined to store the current date and time. The .getMilliseconds() method is used to extract the total milliseconds and both values are printed to the console.

Code
Output
Loading...

All contributors

Contribute to Docs

Learn JavaScript on Codecademy