How to Get the Current Date in JavaScript ?
Last Updated :
28 Oct, 2024
Improve
The Date() object is used to access the current date in JavaScript. To get the current date in string format, we can use toDateString() method. The date.toDateString() method converts the given date object into the date portion into a string.
Syntax
dateObj.toDateString()
// Create a Date Object
let d = new Date();
let d1 = d.toDateString();
console.log(d1);
Output
Mon Oct 28 2024
Example: By default, the Date() constructor display the complete date as a string.
// Create a Date Object
let d = new Date();
console.log(d);
Output
2024-10-28T07:56:14.317Z
Extracting Day, Month, and Year from Date Object
You can extract individual components of date like —day, month, and year from the Date object.
- The getDate() method extract the date from Date object.
let d = new Date();
let date = d.getDate();
- The getMonth() method extract the month (0 to 11) from the Date object.
let d = new Date();
let month = String(d.getMonth() + 1).padStart(2, '0');
- The getFullYear() method extract the year from Date object.
let d = new Date();
let year = d.getFullYear();
Example: Getting the current date (day, month, and year) from the date object.
let d = new Date();
let day = String(d.getDate()).padStart(2, "0");
let month = String(d.getMonth() + 1).padStart(2, "0");
let year = d.getFullYear();
let d1 = day + "/" + month + "/" + year;
console.log(d1);
Output
28/10/2024
We have a complete list of JavaScript Date Objects, to check those please go through this JavaScript Date Object Reference article.