Get the Last Day of the Year in JavaScript

avatar

Borislav Hadzhiev

Mon Oct 25 20212 min read

banner

Photo by Martin Reisch

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 #

Join my newsletter

I'll send you 1 email a week with links to all of the articles I've written that week

Buy Me A Coffee