All TalkersCode Topics

Follow TalkersCode On Social Media

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

JavaScript Date To String Format

Last Updated : Mar 11, 2024

JavaScript Date To String Format

In this article we will show you the solution of JavaScript date to string format, there is various way to convert date to string.

In this article, we are going to use .toDateString(), .toLocaleDateString() and .toLocaleString() methods and also add different properties like day, month, year, hour, minute, second, and time zone to them.

Step By Step Guide On JavaScript Date To String Format :-

We will see different methods in the example below to convert date to string format in JavaScript

Let us see the code first.

<!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 date to string format</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 date to string format </h3>
    <script>
      let date = new Date() ;
      console.log(date.toDateString()) ;
      console.log(date.toLocaleDateString()) ;
      console.log(date.toLocaleString()) ;
      console.log(date.toLocaleString('en-GB', {
        year : "numeric" ,
        month : "long" ,
        day : "numeric" ,
        weekday : "long"
      })) ;
      console.log(date.toLocaleString('en-GB', {
        year : "2-digit" ,
        month : "2-digit" ,
        day : "2-digit" ,
        weekday : "short",
        hour : "2-digit" ,
        minute : "2-digit" ,
        second : "2-digit"
      })) ;
    </script>
</body>
</html>
  1. First, we write <! DOCTYPE html> which we used as an 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 above now the <head> tag is used to contain 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. Thirdly, the <body> tag is used to define the webpage body. All the contents to show on the website are written here.
  6. <h1> tag used to add heading here.
  7. Created the <style> tag to add CSS to the HTML page.
  8. To add JavaScript Create a <script> tag.
  9. First create a variable named ‘date’ for a date object.
  10. Now we used .toDateString() with console.log() to get the date as string of weekday month date and then year. In default month came first before the day
  11. Then use date.LocalDateString() with console.log() to get the date with mm/dd/yyyy format and for default month came first before day.
  12. use date.LocalString() with console.log() to get mm/dd/yyyy format with time.
  13. Again use date.LocalString() with console.log() with, en-GB to get day before month and also adding properties to day,month, year, hour, minute, second and timezone as we desired.

Conclusion :-

At last, here in conclusion, here we can say that with this article’s help, we date to string format in JavaScript.

I hope this article on JavaScript date to string format 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 🡪