All TalkersCode Topics

Follow TalkersCode On Social Media

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

JavaScript Get Date Without Time

Last Updated : Sep 5, 2023

JavaScript Get Date Without Time

In this article we will show you the solution of JavaScript get date without time, we can get the date and time in JavaScript using the Date() object. Now to get a date without time, we have to use some date properties of JavaScript.

  • getMonth(): used to get the month from the Date() object
  • getDate(): used to get the date from the Date() object
  • getFullYear(): used to get the full year from the Date() object

Now we also going to use the toLocaleString() method to convert the date to a string to get the date without time.

Step By Step Guide On JavaScript Get Date Without Time :-

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JavaScript get date without time</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/water.css">
    <style>
        h1 {
          color : rgb(95, 194, 95) ;
          font-size: 25px;
          font-weight : bolder ;
        }
    </style>
</head>
<body>
    <center>
        <h1> TALKERSCODE </h1>
        <h3> JavaScript get date without time </h3>
    </center>
    <Script>
        var myDate= new Date() ;
        document.write(myDate + '<br>') ;
        var month = myDate.getMonth() + 1 ;
        var date = myDate.getDate() ;
        var year = myDate.getFullYear() ;
        document.write(month + '/' + date + '/' + year + '<br>') ;
        document.write(myDate.toLocaleString('en-GB', {
        year : "numeric" ,
        month : "long" ,
        day : "numeric" ,
        weekday : "long"
      })) ;
    </Script>
</body>
</html>
  1. To write HTML code <! DOCTYPE html> to mention which version of the HTML file is written in.
  2. Then we have to write the <html> tag used to define the root of an HTML document. And also write the ending tag </html>.
  3. <head> tag used here to use to contain the metadata about the HTML file and end it with </head> tag
  4. Then <title> tag is used to set the title of the HTML document and end it with </title> tag.
  5. In order to style the HTML page, we'll utilize an external CSS file
  6. using <style> tag to add CSS
  7. <h1> tag used to add a heading close it with </h1> tag
  8. Create a <Script> tag to write JavaScript within it.
  9. Using var to create a variable myDate for the new Date() object
  10. Document.write() to see the date and time
  11. Now using getMonth() to get the month. But we have to add 1 to the month because the month count starts from 0
  12. Use getDate() to get a date and getFullYear() to get the full year.
  13. Using document.write() to display the date in the HTML page into mm/dd/yyyy format
  14. Also using toLocaleString() method to get the weekday, date, month and year.

Conclusion :-

At last, here in conclusion, we can say that with this article’s help, we know how to get date without time in JavaScript.

I hope this article on JavaScript get date without time helps you and the steps and method mentioned above are easy to follow and implement.