Get the First and Last Day of a Year in JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 6, 2024
3 min

banner

# Get the First and Last Day of a Year in JavaScript

Use the Date() constructor to get the first and last day of a year.

The Date() constructor takes the year, a zero-based value for the month and the day as parameters and returns a Date object.

index.js
// โœ… Get the first and last day of the current year const currentYear = new Date().getFullYear(); console.log(currentYear); // ๐Ÿ‘‰๏ธ 2023 const firstDay = new Date(currentYear, 0, 1); console.log(firstDay); // ๐Ÿ‘‰๏ธ Sun Jan 01 2023 const lastDay = new Date(currentYear, 11, 31); console.log(lastDay); // ๐Ÿ‘‰๏ธ Sun Dec 31 2023

get first and last day of year

The code for this article is available on GitHub

You can use this approach to get the first and last day of any year.

Here is a reusable function that takes the year as a parameter and returns the last day of the year.

index.js
function getLastDayOfYear(year) { return new Date(year, 11, 31); } // ๐Ÿ‘‡๏ธ โœ… Get the last day of the current year const currentYear = new Date().getFullYear(); console.log(getLastDayOfYear(currentYear)); // ๐Ÿ‘‰๏ธ Sun Dec 31 2023 // ๐Ÿ‘‡๏ธ โœ… Get the last day of any year console.log(getLastDayOfYear(2025)); // ๐Ÿ‘‰๏ธ Wed Dec 31 2025

get last day of year

The code for this article is available on GitHub

And here is a function that takes the year as a parameter and returns the first day of the year.

index.js
function getFirstDayOfYear(year) { return new Date(year, 0, 1); } // ๐Ÿ‘‡๏ธ โœ… Get the first day of the current year const currentYear = new Date().getFullYear(); console.log(getFirstDayOfYear(currentYear)); // ๐Ÿ‘‰๏ธ Sun Jan 01 2023 // ๐Ÿ‘‡๏ธ โœ… Get the first day of any year console.log(getFirstDayOfYear(2025)); // ๐Ÿ‘‰๏ธ Wed Jan 01 2025

get first day of year

We passed the following 3 arguments to the Date():

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

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

index.js
console.log(new Date().getFullYear()); // ๐Ÿ‘‰๏ธ 2023
The value of the month is zero-based, where January is 0, February is 1, March is 2, and so on, up to December, which is 11.

To get the first day of the current year, we passed 0 (January) for the month and 1 for the day of the month to the Date() constructor.

index.js
const currentYear = new Date().getFullYear(); console.log(currentYear); // ๐Ÿ‘‰๏ธ 2023 const firstDay = new Date(currentYear, 0, 1); console.log(firstDay); // ๐Ÿ‘‰๏ธ Sun Jan 01 2023

To get the last day of the current year, we passed 11 (December) for the month and 31 for the day of the month.

index.js
const currentYear = new Date().getFullYear(); console.log(currentYear); // ๐Ÿ‘‰๏ธ 2023 const lastDay = new Date(currentYear, 11, 31); console.log(lastDay); // ๐Ÿ‘‰๏ธ Sun Dec 31 2022
We specified 31 for the day to get the last day of the year. A calendar year always starts on January 1st and ends on December 31st.

# Getting the First and Last day of any year

You can use this approach to get the first and last day of any year. All you have to do is change the first parameter in the call to the Date() constructor.

index.js
const year = 2030; const firstDay = new Date(year, 0, 1); console.log(firstDay); // ๐Ÿ‘‰๏ธ Tue Jan 01 2030 const lastDay = new Date(year, 11, 31); console.log(lastDay); // ๐Ÿ‘‰๏ธ Tue Dec 31 2030

get first and last day of any year

The code for this article is available on GitHub

If you want to make your code more readable, store the values for the month in variables with appropriate naming.

index.js
const currentYear = new Date().getFullYear(); const january = 0; const firstDay = new Date(currentYear, january, 1); console.log(firstDay); // ๐Ÿ‘‰๏ธ Sat Jan 01 2022 const december = 11; const lastDay = new Date(currentYear, december, 31); console.log(lastDay); // ๐Ÿ‘‰๏ธ Sat Dec 31 2022

Defining separate variables that store the zero-based value for the month makes our code a bit more readable.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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.

Copyright ยฉ 2024 Borislav Hadzhiev