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

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>                    

chevron_right


Output:
toLocaleDateString

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>                    

chevron_right


Output:
toISOString



My Personal Notes arrow_drop_up

Technical Writer Lets have an update

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.