All TalkersCode Topics

Follow TalkersCode On Social Media

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

How To Get Current Year In JavaScript

Last Updated : Jul 1, 2023

How To Get Current Year In JavaScript

In this tutorial we will show you the solution of how to get current year in JavaScript, in last tutorial we understand that how to get current date with the help of JavaScript, we hope you understand that article properly.

Now, let us understand how to get current year in JavaScript with help of example.

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

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

But in this article we use only inbuilt JavaScript function or tools with the help of which we get current year. Here, below we are going to show you some codes in JavaScript to print current year.

<!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>
 <h1 style="color: green">
  Talkers code
 </h1>
 <h3>How to get current year in JavaScript</h3>
 <p id=”displayYear"></p>
 <button onclick="getYear()">Get Date</button>
 <script type="text/javascript">
 function getYear() {
  // Creating Date Object here
  var date_here = new Date();
  // get Year from above object
  // here we are going to use getFullYear() function that is inbuilt function.
  var getYear= date_here.getFullYear();
  // Printing current year
  document.getElementById("displayYear").innerHTML =
  "The Current Year is : " + getYear; }
 </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, 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, again the content is same like get date article. Here, we first create two headings for better look.
  6. After this we create a paragraph with id and a button also.
  7. Then we create script tag, which is paired tag. and write our JavaScript codes under this tag.
  8. Here, we create a function that runs on button click and inside this function, we get full date with help of date function and store in it date_here.
  9. Then we again create a variable getYear and store current date or say extract current year from date_here with help of getFullYear() function.
  10. Now, our last job is to print this stored year. So, we use our paragraph with help of id and print date with help of inner.HTML. We hope that you understand this article properly.
  11. 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 year with JavaScript.

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

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪