Horje
Difference Between new Date() and Date.now() in JavaScript

In JavaScript, handling dates and times is essential for various applications from displaying the current date to performing complex calculations involving time differences. Two commonly used methods for working with the dates are new Date() and Date.now(). Despite dealing with the time they serve different purposes and have distinct behaviors.

These are the following topics that we are going to discuss:

What is the new Date()?

The new Date() constructor in JavaScript creates a new Date object that represents the current date and time or specifies the date and time. It provides a wide range of methods for working with dates including retrieving various components such as the year, month, day, hour, minute, second, and milliseconds.

Characteristics:

  • Returns a Date object initialized with the current date and time if no arguments are provided.
  • Can accept various formats for creating dates such as the specifying the year, month, day and time components individually or together.
  • Provides methods like getDate(), getMonth(), getFullYear(), getHours() etc. to the retrieve specific parts of the date.

Applications:

  • Displaying the current date and time on a web page.
  • Performing the date arithmetic and manipulation .
  • Formatting dates for the user-friendly output based on the local conventions.

Example: This example shows the use of new Date() constructor.

JavaScript
const currentDate = new Date();
console.log(currentDate);

Output:

Thu Jul 04 2024 14:32:00 GMT+0530 (India Standard Time)

What is Date.now()?

The Date.now() method returns the numeric value corresponding to the current time—the number of the milliseconds elapsed since January 1, 1970, 00:00:00 UTC. It provides a simple way to obtain a timestamp suitable for measuring performance or calculating time differences.

Characteristics:

  • Returns a numeric value representing the current time.
  • Does not create a Date object, it directly returns the timestamp.
  • Suitable for measuring elapsed time or benchmarking operations.

Applications:

  • The Benchmarking code performance by calculating the time taken to execute a function.
  • Generating unique identifiers or timestamps for the data records.
  • Recording the time when an event occurs for the logging purposes.

Example: This example shows the use of Date.now() method.

JavaScript
const timestamp = Date.now();
console.log(timestamp);

Output:

1718695511541

Difference Between new Date() and Date.now()

Characteristics

new Date()

Date.now()

Return Type

Date object

Number

Initialization

Returns current date/time if no arguments are provided.

N/A (direct timestamp)

Usage

The Date manipulation and formatting retrieving date components.

Measuring performance creating timestamps.

Example

new Date() returns a Date object representing the current date/time.

Date.now() returns the current timestamp in milliseconds.




Reffered: https://www.geeksforgeeks.org


JavaScript

Related
How to Fix npm ERR! code EINTEGRITY? How to Fix npm ERR! code EINTEGRITY?
JavaScript SyntaxError – Octal literals are not allowed in strict mode JavaScript SyntaxError – Octal literals are not allowed in strict mode
JavaScript SyntaxError – Unexpected end of input JavaScript SyntaxError – Unexpected end of input
Difference Between Object.keys() and Object.getOwnPropertyNames() in JavaScript Difference Between Object.keys() and Object.getOwnPropertyNames() in JavaScript
Difference Between Bearer Token and Basic Authentication Difference Between Bearer Token and Basic Authentication

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
20