JavaScript getMilliseconds

The JavaScript getMilliseconds function is one of the date methods that returns the milliseconds value from the given date according to the local time.

Syntax

The syntax of the JavaScript getMilliseconds() function is

dateObject.getMilliseconds()

Return Value: The getMilliseconds() method returns an integer between 0 and 999 representing the milliseconds from the given date and time. For any invalid date object, it returns NaN.

JavaScript getMilliseconds Example

Example 1: Get Milliseconds from Current Date

In the following example, we use the getMilliseconds() function to extract the milliseconds value from the current date and time.

const dt = new Date();
const ms = dt.getMilliseconds()
console.log(ms);
console.log(dt);
964
2026-06-07T03:37:05.964Z

Example 2: Extract Milliseconds from Custom Date

In the example below, we use the Date() constructor to create a custom date. Next, we use the JavaScript getMilliseconds() function to extract the milliseconds from the custom date and time.

const dt = new Date("December 22, 2016 10:19:45.314");
console.log(dt.getMilliseconds());
314

Example 3: In this JavaScript example, we are trying to extract the milliseconds from a custom date without (time). As there is no time value, the getMilliseconds method will return 0 milliseconds.

const dt = new Date('December 22, 1972');
console.log(dt.getMilliseconds());
0

Example 4: If you use the getMilliseconds() method with a date and time without a milliseconds value, it returns 0.

const dt = new Date('December 10, 2025 10:19:45');
console.log(dt.getMilliseconds());
0

Example 5: When we build a custom date with a milliseconds value above 999, the JavaScript getMilliseconds() function returns 0. Here, we used 1500 as the milliseconds value, but the number should be below 999, so it returns 0.

NOTE: The Date() constructor sets the milliseconds value to 0, and the getMilliseconds() extracts that 0 value as the output.

const dt = new Date('January 1,2025 10:19:45:1500');
console.log(dt.getMilliseconds());
0

Example 6: In this example, we use the getMilliseconds() on an invalid date to check the NaN output. If you use this method against any invalid date, the result is NaN.

const dt = new Date('February 32,2025 10:19:45:150');
console.log(dt.getMilliseconds());
NaN

JavaScript Date get milliseconds from 1970

We can use either the Date.now() function or the getTime() method, and both get the total number of milliseconds from 1970 to the given date.

const dt = new Date()
console.log(dt);

const milliseconds = Date.now();
console.log(milliseconds);
console.log(dt.getTime());
2026-06-07T03:57:02.956Z
1780804622963
1780804622956

JavaScript get milliseconds difference between two dates

If we have two distinct dates (Start and End) and calculate the difference between them, it will return the difference in milliseconds (by default).

const start = new Date("2026-06-13T10:00:00");
const end = new Date("2026-06-13T12:30:00");

const milliseconds = (end - start);
console.log(milliseconds)
9000000