JavaScript Get Date Without Time
Last Updated : Mar 11, 2024
IN - JavaScript | Written & Updated By - Ashish
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>
- To write HTML code <! DOCTYPE html> to mention which version of the HTML file is written in.
- Then we have to write the <html> tag used to define the root of an HTML document. And also write the ending tag </html>.
- <head> tag used here to use to contain the metadata about the HTML file and end it with </head> tag
- Then <title> tag is used to set the title of the HTML document and end it with </title> tag.
- In order to style the HTML page, we'll utilize an external CSS file
- using <style> tag to add CSS
- <h1> tag used to add a heading close it with </h1> tag
- Create a <Script> tag to write JavaScript within it.
- Using var to create a variable myDate for the new Date() object
- Document.write() to see the date and time
- Now using getMonth() to get the month. But we have to add 1 to the month because the month count starts from 0
- Use getDate() to get a date and getFullYear() to get the full year.
- Using document.write() to display the date in the HTML page into mm/dd/yyyy format
- 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.