Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to get current year in JavaScript

In this blog post, we'll learn how to get the current year in JavaScript. This is a simple yet important task when working with date and time in web development. By the end of this tutorial, you'll be able to retrieve the current year and manipulate it as needed for your own projects.

JavaScript is a versatile programming language, and understanding how to work with date and time is crucial for many applications. Whether you want to display the current year on a website, calculate someone's age, or make a countdown timer for an event, you'll find this tutorial helpful.

Getting Started

Before we dive into the actual code, let's make sure we're on the same page with some fundamental concepts. Don't worry if you're new to programming; we'll explain everything in simple terms.

What is JavaScript?

JavaScript is a programming language used primarily for web development. It allows you to add interactivity and dynamic content to your website. When you write JavaScript code, you're essentially giving instructions to the web browser on how to respond to different events, update the page content, or perform various calculations.

Variables

In programming, we often need to store and manipulate data. A variable is like a container that holds a piece of information. You can think of it as a labeled box where you can store any value you want. In JavaScript, you can create a variable using the let keyword, followed by the variable name and an equal sign (=), like this:

let myVariable = 10;

In this example, we created a variable called myVariable and assigned it the value 10.

Now that we've covered some basics, let's move on to our main topic: getting the current year in JavaScript.

The Date Object

JavaScript has a built-in object called Date that allows you to work with date and time. The Date object can be used to get information about the current date and time, as well as to perform various calculations and manipulations on dates.

To create a new Date object representing the current date and time, you can simply call the Date constructor without any arguments:

let currentDate = new Date();

Now, currentDate holds a Date object representing the current date and time. The Date object has several methods (functions) that you can use to extract different pieces of information from it. In our case, we're interested in getting the current year.

Getting the Current Year

To get the current year, we can use the getFullYear() method of the Date object. This method returns the full, four-digit year of the specified date. Here's how you can use it:

let currentDate = new Date();
let currentYear = currentDate.getFullYear();
console.log(currentYear);

In this example, we first created a Date object representing the current date and time, and then we called the getFullYear() method on it to get the current year. Finally, we used the console.log() function to print the current year to the browser's console.

If you run this code in your browser's JavaScript console or in a script on your web page, you should see the current year printed to the console.

Using the Current Year

Now that we know how to get the current year in JavaScript, let's see how we can use this information in a real-world example. Let's say you want to display a copyright notice on your website with the current year. Here's how you can do it:

  1. Add an HTML element to your web page where you want to display the current year. Give it an id attribute so that you can easily select it with JavaScript:
<footer>
  &copy; <span id="currentYear"></span> Your Company Name. All rights reserved.
</footer>
  1. Add a script to your web page that gets the current year and updates the content of the specified element:
<script>
  // Get the current year
  let currentDate = new Date();
  let currentYear = currentDate.getFullYear();

  // Find the element with the id "currentYear"
  let currentYearElement = document.getElementById("currentYear");

  // Update the content of the element with the current year
  currentYearElement.textContent = currentYear;
</script>

In this example, we first got the current year using the Date object and the getFullYear() method, just like before. Then, we used the document.getElementById() function to find the HTML element with the id currentYear. Finally, we updated the content of this element with the current year using the textContent property.

If you include this code in your web page, you should see the current year displayed in the copyright notice.

Conclusion

In this tutorial, we've learned how to get the current year in JavaScript using the Date object and the getFullYear() method. We also saw how to use this information to display the current year on a web page.

You can now use this knowledge to add dynamic date information to your websites, perform date calculations, or create countdown timers. JavaScript's Date object offers many other methods for working with date and time, so be sure to explore its full potential as you continue learning about web development.

Happy coding!