How to get the current date in JavaScript ?

11 Nov, 2021
In this article, we will know how to get the current date using the built-in functions 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, year.
There are several other ways to perform this task but here, we will understand these 2 ways for 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.
HTML
<!DOCTYPE html><html>  <head>    <title>How to get current date in JavaScript?</title></head>  <body>    <h1 style="color: green">        GeeksforGeeks    </h1>     <b>How to get current date in JavaScript?</b>      <p> Current Date is: <span class="output"></span></p>      <button onclick="getCurrentDate()">Get current Date</button>          <script type="text/javascript">    function getCurrentDate() {        let date = new Date().toDateString();        document.querySelector('.output').textContent = date;    }    </script></body>  </html>
Output:
toDateString() Method
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.
HTML
<!DOCTYPE html><html>  <head>    <title> How to get current date in JavaScript? </title></head>  <body>    <h1 style="color: green">        GeeksforGeeks    </h1>    <b>        How to get current date in JavaScript?    </b>      <p> Current Date is: <span class="output"></span> </p>      <button onclick="getCurrentDate()"> Get current Date </button>          <script type="text/javascript">        function getCurrentDate() {            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;            document.querySelector('.output').textContent = output;        }    </script></body>  </html>
Output:
Getting the current Date
bhaskargeeksforgeeks
JavaScript-Questions
Picked
JavaScript
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.