Last updated: Mar 6, 2024
Reading timeยท3 min
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.
// โ 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
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.
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
And here is a function that takes the year as a parameter and returns the first day of the year.
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
We passed the following 3 arguments to the Date():
We used the Date.getFullYear() method to get the current year.
console.log(new Date().getFullYear()); // ๐๏ธ 2023
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.
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.
const currentYear = new Date().getFullYear(); console.log(currentYear); // ๐๏ธ 2023 const lastDay = new Date(currentYear, 11, 31); console.log(lastDay); // ๐๏ธ Sun Dec 31 2022
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.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.
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
If you want to make your code more readable, store the values for the month in variables with appropriate naming.
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.
You can learn more about the related topics by checking out the following tutorials: