How to get the current year in JavaScript?

How to get the current year in JavaScript?

Hello Prynka,

Using the Date object: This creates a new Date object representing the current date and time, and then uses the getFullYear() method to extract the year.

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

Hey Pryanka,

Using Date.now() and new Date(): This approach also creates a new Date object, but it uses Date.now() to get the current timestamp, which is then passed to the Date constructor.

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

Hello Prynka,

Using toLocaleDateString(): This method returns a string representing the date portion of the Date object in a localized format, including just the year in this case.

const currentYear = new Date().toLocaleDateString(‘en-US’, { year: ‘numeric’ });