Javascript convert string to date format dd/mm/yyyy

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    The task is to format the current date in dd/mm/yyyy format by using JavaScript. We’re going to discuss few methods.
    First few methods to know

    • JavaScript getDate() Method:
      This method returns the day of the month (from 1 to 31) for the defined date.
      Syntax:
      Date.getDate()
      

      Return value:
      It returns a number, from 1 to 31, representing the day of the month.

    • JavaScript getFullYear() Method:
      This method returns the year (four digits for dates between year 1000 and 9999) of the defined date.
      Syntax:
      Date.getFullYear()
      

      Return value:
      It returns a number, representing the year of the defined date

    • JavaScript getMonth() Method:
      This method returns the month (from 0 to 11) for the defined date, based on to local time.
      Syntax:
      Date.getMonth()
      

      Return value:
      It returns a number, from 0 to 11, representing the month.

    • JavaScript String slice() method:
      This method gets parts of a string and returns the extracted parts in a new string.
      It uses the start and end parameters to define the part of the string to extract.
      First character starts from position 0, the second has position 1, and so on.
      Syntax:
      string.slice(start, end)
      

      Parameters:

      • start: This parameter is required. It specifies the position from where to start the extraction. First character is at position 0
      • end: This parameter is optional. It specifies the position (excluding it) where to stop the extraction. If not used, slice() selects all characters from the start-position till the end of string.

      Return value:
      It returns a string, representing the extracted part of the string.

    • replace() method:
      This method searches a string for a defined value, or a regular expression, and returns a new string with the replaced defined value.
      Syntax:
      string.replace(searchVal, newvalue)
      

      Parameters:

      • searchVal: This parameter is required. It specifies the value, or regular expression, that is going to replace by the new value.
      • newvalue: This parameter is required. It specifies the value to replace the search value with.

      Return value:
      Returns a new string where the defines value(s) has been replaced by the new value.

    Example 1: This example format the date in dd/mm/yyyy by checking both date and month, If they are not in 2 digits the zero is added to make them 2 digits.

    <!DOCTYPE HTML>

    <html>

    <head>

        <title>

            JavaScript 

          | How to get current formatted date dd/mm/yyyy.

        </title>

    </head>

    <body style="text-align:center;"

          id="body">

        <h2 style="color:green;">  

                GeeksForGeeks  

            </h2>

        <p id="GFG_UP" 

           style="font-size: 15px; 

                  font-weight: bold;">

        </p>

        <button onclick="gfg_Run()">

            get Date

        </button>

        <p id="GFG_DOWN" 

           style="color:green; 

                  font-size: 20px; 

                  font-weight: bold;">

        </p>

        <script>

            var el_up = document.getElementById("GFG_UP");

            var el_down = document.getElementById("GFG_DOWN");

            var today = new Date();

            el_up.innerHTML = today;

            var dd = today.getDate();

            var mm = today.getMonth() + 1;

            var yyyy = today.getFullYear();

            if (dd < 10) {

                dd = '0' + dd;

            }

            if (mm < 10) {

                mm = '0' + mm;

            }

            var today = dd + '/' + mm + '/' + yyyy;

            function gfg_Run() {

                el_down.innerHTML = today;

            }

        </script>

    </body>

    </html>

    Output:

    Example 2: This example first slice the date part from the date object and then format the date in dd/mm/yyyy.

    <!DOCTYPE HTML>

    <html>

    <head>

        <title>

            JavaScript 

          | How to get current formatted date dd/mm/yyyy.

        </title>

    </head>

    <body style="text-align:center;"

          id="body">

        <h2 style="color:green;">  

                GeeksForGeeks  

            </h2>

        <p id="GFG_UP" 

           style="font-size: 15px;

                  font-weight: bold;">

        </p>

        <button onclick="gfg_Run()">

            get Date

        </button>

        <p id="GFG_DOWN"

           style="color:green; 

                  font-size: 20px; 

                  font-weight: bold;">

        </p>

        <script>

            var el_up = document.getElementById("GFG_UP");

            var el_down = document.getElementById("GFG_DOWN");

            var today = new Date();

            el_up.innerHTML = today;

            function gfg_Run() {

                var date = today.toJSON().slice(0, 10);

                var nDate = date.slice(8, 10) + '/' 

                           + date.slice(5, 7) + '/' 

                           + date.slice(0, 4);

                el_down.innerHTML = nDate;

            }

        </script>

    </body>

    </html>

    Output:

    JavaScript is best known for web page development but it is also used in a variety of non-browser environments. You can learn JavaScript from the ground up by following this JavaScript Tutorial and JavaScript Examples.


    How convert dd mm yyyy string to Date in Javascript?

    To convert a dd/mm/yyyy string to a date: Split the string on each forward slash to get the day, month and year. Pass the year, month minus 1 and the day to the Date() constructor. The Date() constructor creates and returns a new Date object.

    How do you convert a string to a Date in Javascript?

    Use the Date() constructor to convert a string to a Date object, e.g. const date = new Date('2022-09-24') . The Date() constructor takes a valid date string as a parameter and returns a Date object.

    What Date format is dd mm yyyy in Javascript?

    To format a date as dd/mm/yyyy: Use the getDate() , getMonth() and getFullYear() methods to get the day, month and year of the date. Add a leading zero to the day and month digits if the value is less than 10 .

    How do you format a Date mm dd yyyy?

    First, pick the cells that contain dates, then right-click and select Format Cells. Select Custom in the Number Tab, then type 'dd-mmm-yyyy' in the Type text box, then click okay.