Get the Last Day of the Year in JavaScript

avatar

Borislav Hadzhiev

Last updated: Oct 25, 2021

banner

Photo from Unsplash

Get the Last Day of the Year #

To get the last day of the year, use the Date() constructor to create a date object, passing it a call to the getFullYear() method for the year, 11 for the month, and 31 for the day as parameters. The Date object will contain the last day of the year.

index.js
function getLastDayOfYear(year) { return new Date(year, 11, 31); } // 👇️ Current Year const currentYear = new Date().getFullYear(); console.log(getLastDayOfYear(currentYear)); // 👉️ Sat Dec 31 2022 // 👇️ Year 2025 console.log(getLastDayOfYear(2025)); // 👉️ Wed Dec 31 2025

We passed the following 3 parameters to the Date() constructor:

  1. The year
  2. The month
  3. The day

We used the Date.getFullYear method to get the current year.

For the month, we hard coded 11 because we want the last month of the year.

Months are zero-based in JavaScript, meaning 0 is January and 11 is December.
We provided 31 for the day to get the last day of the year. A calendar year always starts on January 1st and ends December 31st.

The getLastDayOfYear function works for any year, here are some examples.

index.js
function getLastDayOfYear(year) { return new Date(year, 11, 31); } // 👇️ Current Year const currentYear = new Date().getFullYear(); console.log(getLastDayOfYear(currentYear)); // 👉️ Sat Dec 31 2022 console.log(getLastDayOfYear(2030)); // 👉️ Tue Dec 31 2030 console.log(getLastDayOfYear(2035)); // 👉️ Mon Dec 31 2035 console.log(getLastDayOfYear(2040)); // 👉️ Mon Dec 31 2040

The only tricky thing here is to remember that months are zero-based and go from 0 (January) to 11 (December).

If you want to make the function easier to read and more intuitive, you can extract the digit 11 in a variable, like so.

index.js
function getLastDayOfYear(year) { const december = 11; return new Date(year, december, 31); }

Further Reading #

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.