All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Convert String To Date In JavaScript

Last Updated : Jul 1, 2023

How To Convert String To Date In JavaScript

In this tutorial we will show you the solution of how to convert string to date in JavaScript, here we needs to convert specified particular date string to correct date format during creating date object.

We can give any date as per our own thought but the format of ‘-’ between dates must be same otherwise we fail to get whole format of date correctly then result printed on both console and webpage.

Step By Step Guide On How To Convert String To Date In JavaScript :-

Here we defined two div element with id’s ‘res1,res2’ for append results on webpage.

In script we defined variable ‘str’ for holds specified date and we passed that specified date ‘str’ as parameter in creation of date object.

When creation of date object had any values that is take it as parameter then it will convert that value into date otherwise it will returns current date with whole format.

Finally we printed results on both console and webpage.

<!DOCTYPE html>
<html>
    <head>
        <title>CONVERT STRING TO DATE</title>
    </head>
    <body>
        <div id="res1">String : </div>
        <div id="res2">Converted Date : </div>
        <script>
            const str="2022-09-24";
            const date=new Date(str);
            document.getElementById("res1").innerHTML+=str;
            document.getElementById("res2").innerHTML+=date;
            console.log(date);
        </script>
    </body>
</html>
  1. <!DOCTYPE html> tag which is instruct the web browser about what version of HTML file written in and it’s not have any ending tag.
  2. The<html> tag is used to indicate the beginning of HTML document.
  3. As above shown <head> tag is contain information about webpage and external file links are declared here. <title> tag is used for set the webpage title.
  4. Both <head> and <title> tags having their pair end tag, so we need to close the ending tags respectively. If you’re not closed anyone of ending tag properly that is also affect the webpage result.
  5. <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
  6. Here we defined two div elements with id’s ‘res1,res2’ for appends result on webpage by innerHTML.
  7. In script variable ‘str’ defined with const keyword there we storing date string ‘2022-09-24’ then we passing variable ‘str’ to date object creation for convert ‘str’ to date format.
  8. Then we appended specified string ‘str’ on webpage by using ‘res1’ id attribute defined in div element with string ‘String :’ that’s way we added ‘+’ plus symbol at end of ‘innerHTML’ property.
  9. We appended converted ‘date’ on webpage by using ‘res2’ id attribute defined in div element with string ‘Converted Date :’ that’s way we added ‘+’ plus symbol at end of ‘innerHTML’ property and also we added end result on console.
  10. Both </body>,</html> tags closed respectively. </body> tag indicates the end of body, Then </html> tag indicates the end of HTML document.

Conclusion :-

In conclusion now we are able to know how to convert string to date using javascript.

When we executes program on browser we can see the result messages of ‘String : 2022-09-24, Converted Date : Sat Sep 24 2022 05:30:00 GMT+0530 (India Standard Time)’ on webpage.

For seeing result printed on console panel use shortcut ‘ctrl+shift+j’ will open console panel at right hand side of browser.

I hope this tutorial on how to convert string to date in JavaScript helps you and the steps and method mentioned above are easy to follow and implement.