All TalkersCode Topics

Follow TalkersCode On Social Media

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

JavaScript Get Current Date yyyy-mm-dd

Last Updated : Mar 11, 2024

JavaScript Get Current Date yyyy-mm-dd

In this tutorial we will show you the solution of JavaScript get current date yyyy-mm-dd, one thing to say here that there are many ways with the help of which you are able to print current date.

In last tutorial, we understand that how do we print current date with time with the help of JavaScript. We hope that you understand this article properly. Now, let us understand our today’s article.

Step By Step Guide On JavaScript Get Current Date yyyy-mm-dd :-

As, we already said that there are many ways to done this article. We use a method of our previous article in which we learn about how to print current date and time with the help of JavaScript.

And we also use some more inbuilt features of JavaScript here.

<!DOCTYPE html>
<html lang="en">
<head>
    <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>
</head>
<body>
  <script>
    var today = new Date();
    var dd = today.getDate();
    var mm = today.getMonth() + 1;
    var yyyy = today.getFullYear();
    if (dd < 10) {
      dd = '0' + dd;
    }
    if (mm < 10) {
      mm = '0' + mm;
    }
    today = yyyy + '-' + mm + '-' + dd;
    console.log(today);
  </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 <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, in article our body is empty. We use no any html tag inside body. And we use script tag there as we genuinely do. Now, let us discuss script tag.
  5. Inside script tag, we get current date in variable has name today. And from today variable, we get date, month, year in dd, mm, and yyyy variables respectively. And we reassign value to today in a specified format which we want.
  6. And print our current date in console. You can see current date by inspecting webpage and visit to console.
  7. Here, something left that is if conditions. If conditions are used here to make dates and months like 1 to 9 to 01 to 09 format. Hence, with the help of if condition there our one digit dates and months converted into two digits by adding 0 in front of them.
  8. 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 print or get current date in specified format that is yyyy-mm-dd.

We hope that you understand this article properly. I hope this tutorial on JavaScript get current date yyyy-mm-dd helps you and the steps and method mentioned above are easy to follow and implent.

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪