Categories
JavaScript Answers

How to retrieve the last day of the year in JavaScript?

Spread the love

Sometimes, we want to retrieve the last day of the year in JavaScript.

In this article, we’ll look at how to retrieve the last day of the year in JavaScript.

How to retrieve the last day of the year in JavaScript?

To retrieve the last day of the year in JavaScript, we can use the date methods.

For instance, we write:

const lastDayOfYear = new Date(new Date().getFullYear(), 11, 31)
console.log(lastDayOfYear)

to create a Date instance with the 4 digit year number of the current year that we get from getFullYear.

The 2nd argument is 11 for December and the last argument is 31 for the 31st day of the month.

Therefore, lastDayOfYear is Sat Dec 31 2022 00:00:00 GMT-0800 (Pacific Standard Time) if today’s year is 2022.

Conclusion

To retrieve the last day of the year in JavaScript, we can use the date methods.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *