Skip to main content

How to Get the Current Year in JavaScript

Getting the current year is a very common requirement in web development, essential for tasks like adding a copyright notice to a footer, setting default date ranges, or validating data. The standard and most direct way to do this is by using the Date object's built-in methods.

This guide will teach you how to use new Date().getFullYear() to get the current year. We will also cover how to get the last two digits of the year and show a practical example of how to dynamically add the current year to your website's copyright notice.

The Core Method: new Date().getFullYear()

The process for getting the current year is a simple, two-step operation:

  1. new Date(): When called with no arguments, this constructor creates a new Date object representing the current date and time.
  2. .getFullYear(): This method is then called on the Date object, and it returns a four-digit number for the year.

For example, you need to get the current year as a number. This clean, one-line solution is the best practice.

const currentYear = new Date().getFullYear();

console.log(currentYear); // Output: 2025 (or the current year)
note

This is the most direct and idiomatic way to get the current year.

How to Get the Last Two Digits of the Year

Sometimes, you might need just the two-digit representation of the year (e.g., 23 for 2023).

The logic:

  1. Get the full four-digit year as a number.
  2. Convert the number to a string.
  3. Use the slice(-2) method to extract the last two characters.

Solution:

const fullYear = new Date().getFullYear(); // e.g., 2025

const lastTwoDigits = fullYear.toString().slice(-2);

console.log(lastTwoDigits); // Output: 25

The most common use case for getting the current year is to automatically update the copyright notice in a website's footer, so you don't have to change it manually every January 1st.

For example, you want to display a copyright notice like "© 2023 Your Company" without hardcoding the year.

HTML:

<footer>
<p id="copyright"></p>
</footer>

This script gets the current year and injects it into the footer paragraph.

// Get a reference to the copyright element
const copyrightElement = document.getElementById('copyright');

// Get the current year
const currentYear = new Date().getFullYear();

// Set the text content of the element
copyrightElement.textContent = `© ${currentYear} Your Company Name. All rights reserved.`;
note

This ensures your copyright notice is always up to date.

Conclusion

Getting the current year in JavaScript is a simple and fundamental task.

  • The recommended best practice is to use new Date().getFullYear(). This is the most direct, readable, and reliable method.
  • To get the last two digits of the year, convert the full year to a string and use .slice(-2).
  • This is most commonly used to create a dynamic copyright notice, ensuring your website's footer is always current.