JavaScript .now()
Published Jul 22, 2021Updated Jun 2, 2023
Contribute to Docs
Returns the current time in milliseconds. The milliseconds count begins at 1970/01/01 at 00:00:00 UTC.
Syntax
Date.now();
Note: Since now() is a static method of Date, it will always be used as Date.now().
Example 1
Print the current date in milliseconds:
const time = Date.now();console.log(`The current date in milliseconds is ${time}.`);// Output: The current date in milliseconds is 1626374486136.// Note: your output may vary
Example 2
Calculate the number of months since 1970/01/01:
const time = Date.now();console.log(time);// Output: 1626367528657const minutes = 1000 * 60;const hours = minutes * 60;const days = hours * 24;const months = days * 30;const monthsSince1970 = Math.round(time / months);console.log(monthsSince1970);// Output: 627// Note: your output may vary
Codebyte Example
In this example, the function measures the time taken to execute a task. The code records the start time using Date.now() before the task and again after the task is complete.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn JavaScript on Codecademy
- Front-end engineers work closely with designers to make websites beautiful, functional, and fast.
- Includes 34 Courses
- With Professional Certification
- Beginner Friendly.115 hours
- Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.
- Beginner Friendly.15 hours