All TalkersCode Topics

Follow TalkersCode On Social Media

How To Get Current Date In JavaScript

Last Updated : Jul 1, 2023

How To Get Current Date In JavaScript

In this tutorial we will show you the solution of how to get current date in JavaScript, mainly in JavaScript there are two ways with the help of which you can print date.

First way is to print date with full numbers only and other method is you can print date with text as long date.

Now, you also modify date format according to our own choice in JavaScript, which we understand in next articles. Let us understand this first.

Step By Step Guide On How To Get Current Date In JavaScript :-

As, there are many methods with the help of which we can print current date.

But in this article we use only inbuilt JavaScript function or tools with the help of which we get current date.

Here, below we are going to show you some codes in JavaScript to print current date.

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>home page</title>
 <h1 style="color: green">
  Talkers code
 <h3>How to get current date in JavaScript</h3>
<p> Current Date is: <span class="displayDate"></span></p>
 <button onclick="getDate()">Get Date</button>
 <script type="text/javascript">
 function getDate() {
  let date = new Date().toDateString();
  document.querySelector('.displayDate').textContent = date;
  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 <head> tag is used to contain information about web page. In this tag a <title> tag is used which helps us to specify a webpage title. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  4. Here, then we create a body tag. All the content which we want to show on browser’s screen or display is always written inside this codes.
  5. Here, we write some code to print current date inside body. First of all we create two headings after then the main code starts. Here, we create a paragraph tag in which we print current date and then a button on click a function runs and date gets printed.
  6. And let us understand JavaScript. For JavaScript we use script tag, which is a paired tag and write code inside it.
  7. And here, we create a function which runs on button click. Inside this function, we store current date into a variable name as date with the help of inbuilt function that is Date() and convert it into string also.
  8. So, the date printed in format, for example like: Sun Nov 7 2077.
  9. After this we save the text i.e. date to paragraph with the help of class created. We hope that you understand codes. And in next article we will understand how to get date in number with the help of JavaScript.
  10. At last, the <body> and <html> tags are closed with </body> and </html> respectively.

Conclusion :-

At last in conclusion, here we can say that with the help of this article we are able to get current date with the help of JavaScript code.

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