How do you get the current year using vanilla JavaScript?

You can get that information by using the JavaScript Date object. Specifically, you can use the new Date().getFullYear() method. This will return the four digit year in a YYYY format.

The new Date() method gets today's date:

      var todaysDate = new Date()
	  // 2019-12-03T07:11:20.082Z

The todaysDate variable will have a value similar to the one shown above.

Using that variable, we can get the current year:

      var todaysDate = new Date()
      var currentYear = todaysDate.getFullYear()
      // 2019

As shown above, the currentYear will hold the value for the current year.

You can also combine the two variables shown above to be more succinct:

      var currentYear = new Date().getFullYear()
      // 2019

There you have it! That's how you get the current year in vanilla JavaScript.

Thanks for reading and happy coding!