JavaScript Date getMilliseconds() Method



In JavaScript, the getMilliseconds() method is a built-in function that belongs to the Date object. It is used to retrieve the milliseconds value of a date object, which represents the number of milliseconds elapsed since the last second.

If we don't specify the milliseconds component in the date object, this method defaults to "0". If the Date object is "invalid", this method returns "NaN" as result.

Syntax

Following is the syntax of JavaScript Date getMilliseconds() Method −

getMilliseconds();

This method does not accept any parameters.

Return Value

This method returns an integer value representing the milliseconds component of the given Date object.

Example 1

In the following example, we are demonstrating the basic usage of JavaScript Date getMilliseconds() method −






Output

The above program returns the milliseconds of a date according to the local time.

Example 2

In this example, we are printing the milliseconds value from the specified date value −






Output

The above programs returns integer 154 as milliseconds value.

Example 3

If we doesn't specify the milliseconds component in the provided date, it will return "0" as default value −






Output

As we can see the output, "0" is returned.

Example 4

If the Date object is invalid, this mehtod will return "NaN" as result −






Output

As we can see the output, "NaN" is returned.

Advertisements