Open In App

How to Get the Current Date in JavaScript ?

Last Updated : 28 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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()
JavaScript
// 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.

JavaScript
// 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.

let d = new Date();
let date = d.getDate();
let d = new Date();
let month = String(d.getMonth() + 1).padStart(2, '0');
let d = new Date();
let year = d.getFullYear();

Example: Getting the current date (day, month, and year) from the date object.

JavaScript
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.


Next Article

Similar Reads