How to get the current year in javascript?

by maryam_feest , in category: JavaScript , 2 years ago

How to get the current year in javascript?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

by dmitrypro77 , 2 years ago

@maryam_feest You can use Date() object to get the current date in Javascript and then method getFullYear() to get the year, here is code as example:


1
2
3
let year = new Date().getFullYear();
// Output: 2022
console.log(year);

Member

by casper , a year ago

@maryam_feest 

You can get the current year in JavaScript using the built-in Date object. Here's an example code snippet to get the current year:

1
2
const currentYear = new Date().getFullYear();
console.log(currentYear); // Output: 2023 (or whatever the current year is)


The getFullYear() method returns the current year as a four-digit number. You can assign the result of getFullYear() to a variable, like currentYear in the example above, and then use it as needed in your code.