How to Get The Current Year in JavaScript Step-by-Step Guide

Robin
Updated on April 26, 2023

Sometimes we need the current year to display on our websites or perform other operations with it. Luckily, JavaScript gives you a Date constructor function to create a date object and get the current year from it.

I will walk through how to get the current year in JavaScript using the Date object, along with some code examples to show the complete process.

There are 2 steps in order to obtain the current year easily in JavaScript:

  • Step 1: Create a date object Using the Date constructor.
  • Step 2: Call the getFullYear() method on the date object to retrieve the current year.

What is Date Object in JavaScript?

The Date object is a built-in JavaScript object that allows us to work with dates and times. It provides a variety of methods that we can use to retrieve specific information about a time and date.

For creating a new instance of the Date object in JavaScript simply call the Date() constructor function. By default, it returns an object for the current date and time.

But you can also create a date object for a specific date and time by passing it to the constructor function as an argument.

Also Read: How to Add Hours, Minutes & Seconds to A Date in JavaScript


Getting The Current Year in JavaScript

To get the current year in JavaScript, create a Date object for the current date and time using the Date() constructor and call the getFullYear() method on it.

          const date = new Date()

const currentYear = date.getFullYear()

console.log(currentYear)
        

Step 1: I am creating a date object using the constructor function.

Step 2: I am getting the current year by calling the getFullYear() method on the date object.

This method returns the year in a 4-digit number. But you can also get the last 2 digits if you want.

          const date = new Date()

const currentYear = date.getFullYear()

const shortYear = currentYear.toString().substring(2)

console.log(shortYear)
        

The year you get from the getFullYear() method will be a type number. Therefore, to get the last 2 digits, call the toString() method to convert it to a string.

Then you can use the substring() method and pass "2" as its argument to extract the last 2 digits from the previous 4 digits.

Also Read: 3 Best Ways to Add Days To a Date in JavaScript (Complete Guide)


Getting the Year From a Specific Date in JavaScript

To get the year value from a specific date in Javascript, create the Date object using that date. Then you can call the getFullYear() method on that object to get the year.

          const someDate = new Date('1996-12-09')

const year = someDate.getFullYear()

console.log(year)
// 1996
        

Step 1: I am creating a Date object called someDate for "December 9th, 1996". You can pass any date to the Date() constructor function. This object will represent this specific date instead of the current date.

Step 2: You can call the getFullYear() method on the someDate object to get the 4-digit year value. It will return "1996" from the date.

Also Read: Best Ways to Access Object Properties Dynamically in Javascript


Conclusion

There are many usages of the current year in a website. For example, you can display it for the copyright information. When you get it dynamically, you don't have to update it every year.

JavaScript gives you a built-in method to retrieve the year value from a date. This makes the process very simple and easy.

Just call the getFullYear() method on a Date object created by the Date() constructor function to get the current year in JavaScript. You can also use the same method on a Date object that represents a specific date to get that year.

Related Posts