JavaScript .getTime()

jameskeezer's avatar
Published Jun 10, 2023Updated Jan 17, 2024
Contribute to Docs

The .getTime() method returns the number of milliseconds since the 1st of January 1970.

  • Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.
    • Beginner Friendly.
      15 hours

Syntax

myDate.getTime()

The returned value from .getTime() is a value representing the number of milliseconds since the 1st of January 1970.

Example

In the example the variable myDate stores the number of milliseconds from 1/1/1970 to the current time created using by the .getTime() method on a new Date and logs the result to the console.

const myDate = new Date('2023-01-01');
console.log(myDate.getTime());

This will yield:

1672531200000

Codebyte Example

Below is a runnable codebyte which defines today as today’s date and then converts it into milliseconds by using the .getTime() method. A console.log() then prints the value:

Code
Output

All contributors

Contribute to Docs

Learn JavaScript on Codecademy

  • Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.
    • Beginner Friendly.
      15 hours