![]() |
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: Table of Content 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:
Applications:
Example: This example shows the use of new Date() constructor.
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:
Applications:
Example: This example shows the use of Date.now() method.
Output: 1718695511541 Difference Between new Date() and Date.now()
|
Reffered: https://www.geeksforgeeks.org
JavaScript |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 20 |