Open In App

How to get the current date in JavaScript ?

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will see how to get the current date using the built-in methods in JavaScript & will see their implementation through the examples. There are 2 methods to perform this task:

  • Using Date().toDateString() Method
  • Using their respective method for fetching day, month, and year.

There are several other ways to perform this task but here, we will understand these 2 ways of getting the current date in JavaScript.

Method 1: Using Date().toDateString() method

The toDateString() method is used to return only the date part of a Date object. The first three letters specify the day of the week, the next three letters specify the month name, the next two digits specify the day of the month and the last four digit year specify the year. This method returns a string containing the date in the above format. This is a better method than slicing for the date portion in the Date() object.

Syntax:

new Date().toDateString();

Example: This example describes the use of the Date().toDateString() method to get the current date.

Javascript




let date = new Date().toDateString();
 
console.log(date);


Output

Wed Jun 14 2023

Method 2: Getting the day, month, and year using their respective methods 

All the parts of a date can be found by their respective methods. The day can be extracted by using the getDate() method. The month can be extracted by using the getMonth() method and the year can be extracted by using the getFullYear() method. The day and month are padded with 0s if they are single digits by converting it to a String object and using the padStart() method. This method pads the current string until the resulting string reaches the given length, here it is padded to a length of 2. The final date is then constructed by concatenating all these parts in the format as required.

Syntax:

getDate() method: It is used to fetch the date of a month from a given Date object.

let dateObj = new Date();

getMonth() method: It is used to fetch the month(0 to 11) from given Date object.

let month = String(dateObj.getMonth() + 1).padStart(2, '0');

getFullYear() method: It is used to fetch the year from a given Date object.

let year = dateObj.getFullYear();

To convert the date into a string:

let day = String(dateObj.getDate()).padStart(2, '0');

To specify the date in a particular format:

let output = day + '/' + month + '/' + year;

Example: This example describes fetching the current date using their respective methods.

Javascript




let dateObj = new Date();
 
let month = String(dateObj.getMonth() + 1)
    .padStart(2, '0');
     
let day = String(dateObj.getDate())
    .padStart(2, '0');
 
let year = dateObj.getFullYear();
let output = day + '/' + month + '/' + year;
 
console.log(output);


Output

14/06/2023


Last Updated : 20 Jun, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads