How to get current formatted date dd/mm/yyyy in Javascript and append it to an input?

29 May, 2019
Method 1: Using toLocaleDateString() method: The toLocaleDateString() method is used to return a language sensitive representation of the date part of a Date object as a String. It has two optional parameters, locales and options. The locales parameter can be specified with language tags that are used to display the date according to the format of that locale. The locale has various options that can be used to modify the locale used.
The date can be formatted as ‘dd/mm/yyyy’ by specifying the locale as ‘en-GB’ which sets the locale to ‘British English’.
The date can be set to an input using the valueAsDate property. This property takes in a Date object to be displayed in the date selector by default. The current date can be shown by assigning it a new Date() object.
Syntax:
new Date().toLocaleDateString('en-GB')
Example:
<!DOCTYPE html><html>      <head>    <title>        How to get current formatted date dd/mm/yyyy        in Javascript and append it to an input?    </title></head>  <body>    <h1 style="color: green">        GeeksforGeeks    </h1>          <b>        How to get current formatted date dd/mm/yyyy in         Javascript and append it to an input?    </b>          <p>        Current Date is:         <span class="currDate"></span>    </p>          <p>        Please select the date to start the course:        <input type="date" id="dateInput">    </p>          <script type="text/javascript">            let currDate =                new Date().toLocaleDateString('en-GB');                              let inputDate = new Date();              document.querySelector('.currDate').textContent                    = currDate;                                  document.querySelector('#dateInput').valueAsDate                    = inputDate;    </script></body>  </html>                    
Output:
Method 2: Splicing the string from the toLocalISOString() method: The date can be extracted from the string returned by the toLocalISOString() method. The string returned is sliced from 0 to 10th index by using slice(0, 10), split into separate components by the dash(-) separator, reversed to make the day value first, and then joined back using slash(/) as the separator. This gives the date in the ‘dd/mm/yyyy’ format.
The date can be set to an input using the valueAsDate property. This property takes in a Date object to be displayed in the date selector by default. The current date can be shown by assigning it a new Date() object.
Syntax:
new Date().toISOString().slice(0, 10).split('-').reverse().join('/')
Example:
<!DOCTYPE html><html>      <head>    <title>        How to get current formatted date dd/mm/yyyy        in Javascript and append it to an input?    </title></head>  <body>    <h1 style="color: green">        GeeksforGeeks    </h1>          <b>        How to get current formatted date dd/mm/yyyy in         Javascript and append it to an input?    </b>      <p>        Current Date is:         <span class="currDate"></span>    </p>          <p>        Please select the date to start the course:        <input type="date" id="dateInput">    </p>          <script type="text/javascript">            let currDate =                 new Date().toISOString().slice(0, 10)                    .split('-').reverse().join('/');                                  let inputDate = new Date();              document.querySelector('.currDate').textContent                    = currDate;            document.querySelector('#dateInput').valueAsDate                    = inputDate;    </script></body>  </html>                    
Output:
Picked
JavaScript
Web Technologies
Web technologies Questions
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.