Borislav Hadzhiev
Last updated: Oct 25, 2021
Check out my new book
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.
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:
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.
0
is January and 11
is December.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.
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.
function getLastDayOfYear(year) { const december = 11; return new Date(year, december, 31); }