How to get the Current Year in JavaScript [for copyright]

avatar
Borislav Hadzhiev

Last updated: Mar 3, 2024
4 min

banner

# Table of Contents

  1. Get the Current Year in JavaScript
  2. Get the Last 2 Digits of the current Year
  3. Get the current Year for Copyright using JavaScript

# Get the Current Year in JavaScript

To get the current year:

  1. Use the new Date() constructor to get a Date object.
  2. Call the getFullYear() method on the Date object.
  3. The getFullYear method will return the current year.
index.js
// ๐Ÿ‘‡๏ธ Get current Year const currentYear = new Date().getFullYear(); console.log(currentYear); // ๐Ÿ‘‰๏ธ 2024

get current year

The code for this article is available on GitHub

We used the Date() to get a Date object, on which we can call various methods.

index.js
const date = new Date(); console.log(date); // ๐Ÿ‘‰๏ธ 2024-01-04T05:21:48.504Z console.log(date.getFullYear()); // ๐Ÿ‘‰๏ธ 2024

The last step is to use the Date.getFullYear() method to get the current year.

The method returns the year of the specified date according to local time.

Notice that we created a new date object that represents the current date and time using the new Date() constructor.

If you call the method on a Date object that stores a different date and time, the output is reflected.

All you have to do is pass the specific date into the Date constructor on which you call the methods.

index.js
const date = new Date('January 04, 2025 05:24:07'); const yearOfDate = date.getFullYear(); console.log(yearOfDate); // ๐Ÿ‘‰๏ธ 2025

If you have to do this often, define a reusable function.

index.js
function getCurrentYear() { return new Date().getFullYear(); } const currentYear = getCurrentYear(); console.log(currentYear); // ๐Ÿ‘‰๏ธ 2024

The getCurrentYear function takes no parameters and returns the current year.

If you only need the last 2 digits of the current year, use the slice() method.

index.js
function getCurrentYear() { return String(new Date().getUTCFullYear()).slice(-2); } const currentYear = getCurrentYear(); console.log(currentYear); // ๐Ÿ‘‰๏ธ 24
The code for this article is available on GitHub

We used the String.slice() method to get the last 2 digits of the current year.

# Get the Last 2 Digits of the current Year

If you need to get the last 2 digits of the current year, use the slice() method.

index.js
// ๐Ÿ‘‡๏ธ Get the last 2 digits of the current year const last2 = new Date().getFullYear().toString().slice(-2); console.log(last2); // ๐Ÿ‘‰๏ธ '24' const last2Num = Number(last2); // ๐Ÿ‘‡๏ธ Get last 2 digits of a Year const date = new Date('April 04, 2025 05:24:07'); const last2Again = date.getFullYear().toString().slice(-2); console.log(last2Again); // ๐Ÿ‘‰๏ธ '25'

get last 2 digits of current year

The code for this article is available on GitHub

The only argument we passed to the slice method is the start index - the index at which we start extraction of characters from the string.

We used a negative index of -2 to get the last 2 digits of the year.

# Get the current Year for Copyright using JavaScript

Use the getFullYear() method to get the current year for copyright, e.g. new Date().getFullYear().

The getFullYear method returns the current year when called on the current date.

index.js
const currentYear = new Date().getFullYear(); console.log(currentYear); // ๐Ÿ‘‰๏ธ 2024

Here is an example that injects the current year into an HTML element directly on the page.

index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> </head> <body> <div>Content here</div> <div id="copyright"></div> <script> const paragraph = ` <p> Copyright &copy; ${new Date().getFullYear()} John Doe </p> `; document.getElementById('copyright').innerHTML = paragraph; </script> </body> </html>

get current year for copyright

The code for this article is available on GitHub

And here is an example that displays the current date for copyright in a React.js component using JSX.

Footer.js
export const Footer = () => ( <div> <p> Copyright &copy; {new Date().getFullYear()} John Doe </p> </div> );

The Date() constructor creates a new Date object.

We didn't pass any parameters to the constructor when creating the Date, so it created a Date object that stores the current date.

The Date.getFullYear method returns a number that represents the year of the given date.

If you add the code snippet that uses HTML, make sure to place the script tag at the bottom of the body tag, after all of the DOM elements have been declared.

Placing the script tag before declaring the DOM elements would run it before the elements exist on the page and cause an error.

In the HTML example, we used the innerHTML property to set the inner HTML of the element with id set to copyright.

The paragraph variable uses backticks (not single quotes) to create a template string that stores a p element that contains the copyright message.

Here is how the p element looks on the page.

current year for copyright

My blog uses the same approach to display the current year at the footer.

You could change the structure or the style of the element by editing the HTML code in the template string.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev