How to get the current year in JavaScript?

JavaScript is synonymous with working with Dates. And with that comes a whole host of date manipulation methods. So, with that in mind, how can you get the current year in JavaScript?

We  can get the current year in JavaScript by utilizing a native method called getFullYear().

To use it we can simply create a new Date object and invoke getFullYear on it:

const currentYear = new Date().getFullYear()
// this returns the current year

We can also get the current year according to UTC by leveraging native method getUTCFullYear().

const currentYear = new Date().getUTCFullYear()
// this returns the UTC current year

This might seem trivial but there are situations where many different timezones are interacting with each other in a web application. Getting the current UTC year allows parity across multiple timezones.

Proudly published with Gatsby