All TalkersCode Topics

Follow TalkersCode On Social Media

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

JavaScript New Date From String

Last Updated : Mar 11, 2024

JavaScript New Date From String

In this article we will show you the solution of JavaScript new date from string, there is various way to convert date to string. We will see ten different functions to date to a string using JavaScript.

Date(): by using the function in JavaScript convert the timestamp with a date format.

Date Functions used in this tutorial:

  • toDateString() to get the day month date and year.
  • getFullYear() to get the full year.
  • getMonth() to get the month.
  • getDate() to get the date.
  • getHours() to get the hour.
  • getMinutes() to get Minutes
  • getSeconds() to get Seconds
  • getMiliseconds() to get seconds
  • setMinutes() to set Minutes
  • setDate() to set Date.
  • setUTCHours() to set UTC hours
  • toStrings() to view the date we set before.
  • toISOString() to view the date in ISO standard.
  • toLocalString() to see the date in dd/mm/yyyy format.

Step By Step Guide On JavaScript New Date From String :-

<!DOCTYPE html>
<html lang = " en " >
<head>
    <meta charset = " UTF - 8" >
    <meta http-equiv = " X-UA-Compatible " content = " IE=edge " >
    <meta name = " viewport " content = " width = device-width , initial-scale = 1.0 " >
    <title> JavaScript new date from string </title>
    <style>
        body {
            font-family : 'Lucida Sans', Verdana , sans-serif ;
        }
        h1 {
         font-size : larger ;
         font-weight : bolder ;
         color : rgb(113, 221, 113) ;
         text-align : center ;
        }
        h3 {
            text-align : center ;
        }
    </style>
</head>
<body>
    <h1> TALKERSCODE </h1>
    <h3> JavaScript new date from string </h3>
    <script>
        let d ;
        d = new Date () ;
        d = new Date (1164411006456) ;
        d = new Date ('2022-03-18T11:30:00+10:00') ;
        d = new Date (2022, 3, 18,11, 30, 27, 0) ;
        console.log(d.toDateString ()) ;
        console.log(d.getFullYear ()) ;
        console.log(d.getMonth ()) ;
        console.log(d.getDate ()) ;
        console.log(d.getHours ()) ;
        console.log(d.getMinutes()) ;
        console.log(d.getSeconds ()) ;
        console.log(d.getMilliseconds()) ;
        d.setMinutes (10) ;
        d.setDate (5) ;
        d.setUTCHours (4) ;
        console.log (d.toString ()) ;
        console.log(d.toISOString ()) ;
        console.log(d.toLocaleString ("en-AU")) ;
        console.log(d.toLocaleString ("en-US")) ;
    </script>
</body>
</html>
  1. First, we write <! DOCTYPE html> which we used as the instruction to the web browser about what version of HTML file is written in.
  2. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  3. As mentioned above, the <head> tag contains information about the web page. In this tag, a <title> tag is used which helps us to specify a webpage title.
  4. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  5. Now create a < style > tag to add style to the HTML page.
  6. Thirdly, the <body> tag is used to define the webpage body. All the contents to show on the website are written here.
  7. <h1> tag used to add heading here.
  8. Now create a <script> tag to write the JavaScript file. And close it with </script> tag.
  9. Create a variable ‘d’ with a new Date () object.
  10. Using console.log() to display the date with - toDateString () ,getFullYear () ,getMonth () ,getDate () ,getHours (),getMinutes() ,getSeconds () ,getMiliseconds () ,setMinutes () ,setDate () ,setUTCHours() ,toStrings (),toISOString () and toLocalString() function.

Conclusion :-

At last, here in conclusion, here we can say that with this article’s help, we know how to new date from string using JavaScript.

I hope this article on JavaScript new date from string helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Dikshita

Passionate Electronics and Communication Engineering student with expertise in web development (HTML, CSS, JS,PHP, Bootstrap, React.js) and content writing. Eager problem solver and tech enthusiast, adept at creating engaging web experiences.

Follow Dikshita On Linkedin 🡪