How to Get the First and Last Day of a Year in JavaScript
A common task in programming is to determine the start and end dates of a specific year. This is essential for setting date ranges in reports, validating data, or creating calendar-based applications. The JavaScript Date object provides a simple and direct way to construct these dates.
This guide will teach you how to create Date objects for the first and last day of any given year, including the current year.
The Core Method: The Date Constructor
The key to this task is the new Date(year, monthIndex, day) constructor. It allows you to create a Date object for a specific date by providing its components as numbers.
It's crucial to remember that the monthIndex is zero-based:
0= January1= February- ...
11= December
How to Get the First Day of a Year
The first day of any year is always January 1st.
For example, you need to get a Date object representing the first day of the current year.
// 1. Get the current year.
const currentYear = new Date().getFullYear(); // e.g., 2025
// 2. Create a new Date object for January 1st of that year.
// The month index for January is 0.
const firstDayOfYear = new Date(currentYear, 0, 1);
console.log(firstDayOfYear);
Output:
Wed Jan 01 2025 00:00:00 GMT...
This is a direct and reliable way to get the start of the year.
How to Get the Last Day of a Year
Similarly, the last day of any year is always December 31st.
For example, you need to get a Date object representing the last day of the current year.
// 1. Get the current year.
const currentYear = new Date().getFullYear(); // e.g., 2025
// 2. Create a new Date object for December 31st of that year.
// The month index for December is 11.
const lastDayOfYear = new Date(currentYear, 11, 31);
console.log(lastDayOfYear);
Output:
Wed Dec 31 2025 00:00:00 GMT...
Practical Example: Creating Reusable Functions
For cleaner and more maintainable code, it's a best practice to encapsulate this logic in reusable functions.
For example:
/**
* Returns a Date object for the first day of the given year.
* @param {number} year - The year (e.g., 2025).
* @returns {Date}
*/
function getFirstDayOfYear(year) {
return new Date(year, 0, 1);
}
/**
* Returns a Date object for the last day of the given year.
* @param {number} year - The year (e.g., 2025).
* @returns {Date}
*/
function getLastDayOfYear(year) {
return new Date(year, 11, 31);
}
// Example Usage:
const currentYear = new Date().getFullYear();
const firstDay = getFirstDayOfYear(currentYear);
const lastDay = getLastDayOfYear(currentYear);
console.log(`The first day of ${currentYear} is:`, firstDay);
console.log(`The last day of ${currentYear} is:`, lastDay);
// You can also use it for any other year
const lastDayOf2030 = getLastDayOfYear(2030);
console.log('The last day of 2030 is:', lastDayOf2030);
Output:
The first day of 2025 is: Wed Jan 01 2025 00:00:00 GMT...
The last day of 2025 is: Wed Dec 31 2025 00:00:00 GMT...
The last day of 2030 is: Tue Dec 31 2030 00:00:00 GMT...
Conclusion
Getting the first and last day of a year is straightforward in JavaScript by using the new Date(year, monthIndex, day) constructor.
- To get the first day, use a
monthIndexof0(January) and adayof1. - To get the last day, use a
monthIndexof11(December) and adayof31. - Remember that the
monthIndexis always zero-based.
By creating simple helper functions for these tasks, you can make your date-based logic clean, readable, and easy to reuse.