How to get the current date in JavaScript
Getting the current date in JavaScript is fundamental for timestamps, user interfaces, and date calculations in web applications.
As the creator of CoreUI, a widely used open-source UI library, I’ve implemented date handling in countless dashboard components and form controls.
From my expertise, the most straightforward and reliable solution is to use the new Date()
constructor without arguments.
This method instantly returns a Date object representing the current moment in the user’s local timezone.
Use the new Date()
constructor to get the current date and time.
const now = new Date()
The new Date()
constructor creates a Date object containing the current date and time when called without arguments. This includes the full timestamp with year, month, day, hours, minutes, seconds, and milliseconds. The date is automatically set to the user’s local timezone, making it perfect for displaying current time information in user interfaces.
Best Practice Note:
This is the same approach we use in CoreUI components for real-time dashboards and timestamps.
For UTC time specifically, use new Date().toISOString()
, and for Unix timestamps, use Date.now()
which returns milliseconds since epoch.