Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Convert string into date using JavaScript

  • Last Updated : 14 May, 2019

A string can be converted into a date in JavaScript through the following ways-

  • Creating date object using date string:
    Example-1:

    Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!




    <!DOCTYPE html>
    <html>
      
    <head>
        <title>Page Title</title>
    </head>
      
    <body>
        <center>
            <h1 style="color:green">GeeksforGeeks</h1>
            <p>Convert string into date using JavaScript</p>
            <script>
                //It returns the Day,Month,Date,Year and time
                var d = new Date("May 1,2019 11:20:00");
                document.write(d);
            </script>
        </center>
    </body>
      
    </html>

    Output:

  • Getting the string in DD-MM-YY format using suitable methods:
    We use certain methods such as:
    • getDate-It Returns Day of the month(from 1-31)
    • getMonth-It returns month number(from 0-11)
    • getFullYear-It returns full year(in four digits )

    Example-2:




    <!DOCTYPE html>
    <html>
      
    <head>
        <title>Page Title</title>
    </head>
      
    <body>
        <center>
            <h1 style="color:green">
              GeeksforGeeks</h1>
            <p>Convert string into 
              date using JavaScript</p>
            <script>
                var d = new Date("May 1, 2019 ");
                document.write(formatDate(d));
      
                function formatDate(date) {
                    var day = date.getDate();
                    if (day < 10) {
                        day = "0" + day;
                    }
                    var month = date.getMonth() + 1;
                    if (month < 10) {
                        month = "0" + month;
                    }
                    var year = date.getFullYear();
                    return day + "/" + month + "/" + year;
                }
            </script>
        </center>
    </body>
      
    </html>

    Output:

  • Using toDateString():
    This method returns date portion of Date object in human readable form.
    Example-3:




    <!DOCTYPE html>
    <html>
      
    <head>
        <title>Page Title</title>
    </head>
      
    <body>
        <center>
            <h1 style="color:green">
              GeeksforGeeks</h1>
            <p>
              Convert string into 
              date using JavaScript
          </p>
            <script>
                var date = new Date(2019, 5, 3);
      
                document.write(date.toDateString());
            </script>
        </center>
    </body>
      
    </html>

    OUTPUT
    Output:

Supported Browsers:

  • Google Chrome
  • Firefox
  • Edge
  • Opera
  • Apple Safari



My Personal Notes arrow_drop_up
Recommended Articles
Page :