We can use JavaScript to get the current year using the getFullYear() method. It will return the year in a four-digit format.

var currDate = new Date();
var currYear = currDate.getFullYear();

The value of currYear in the code above would return the current year. The day this post was written was January 16th, 2022, so 2022 would be the value of currYear.

Getting and Displaying the Current Day, Month, and Year with the Help of the JavaScript getFullYear Method

Below we will provide code to get the current date in JavaScript, and let the user see it in a friendly format when they click a button.

Get Date

Today's Date is:

We will first get the current date using new Date(). Once we have the date object, we can then get the day using the getDate() method, the year using the getFullYear() method, and the month using the getMonth() method.

We can make the date look even better by converting the month number into the month as a String, as we demonstrated in the previous section.

function genNewDate(){
  var currDate = new Date();
  var currDay = currDate.getDate();
  var currMonth = currDate.getMonth();
  var currYear = currDate.getFullYear();
  var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
  var monthName = months[currMonth];
  document.getElementById("theDate").innerHTML = monthName + ' ' + currDay + ', ' + currYear;
}

The final code and output for this example is below:

Code Output:

Get Date

Today’s Date is:

Full Code:

Get Date

Today's Date is:

<script> function genNewDate(){ var currDate = new Date(); var currDay = currDate.getDate(); var currMonth = currDate.getMonth(); var currYear = currDate.getFullYear(); var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]; var monthName = months[currMonth]; document.getElementById("theDate").innerHTML = monthName + ' ' + currDay + ', ' + currYear; } </script>

Hopefully this article has been useful in showing how to use JavaScript to get the current year.

Categorized in:

JavaScript,

Last Update: March 20, 2024