How to get the First Day of the Year in JavaScript

avatar

Borislav Hadzhiev

Mon Oct 25 20212 min read

banner

Photo by Jeremy Bishop

Get the first day of the Year #

To get the first day of the year, use the Date() constructor to create a date object, passing it a call to the getFullYear() method for the year, 0 for the month, and 1 for the day as parameters. The Date object will contain the first day of the year.

index.js
function getFirstDayOfYear(year) { return new Date(year, 0, 1); } // ๐Ÿ‘‡๏ธ Current Year const currentYear = new Date().getFullYear(); // ๐Ÿ‘‡๏ธ Sat Jan 01 2022 console.log(getFirstDayOfYear(currentYear)); // ๐Ÿ‘‡๏ธ Year 2025 console.log(getFirstDayOfYear(2025)); // ๐Ÿ‘‰๏ธ Wed Jan 01 2025

We passed the following 3 parameters to the Date() constructor:

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

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

For the month, we hard coded 0, because we want the first month of the year.

Months are zero-based in JavaScript, meaning 0 is January and 11 is December.

We provided 1 for the day, because we want the first day of the year.

The getFirstDayOfYear function works for any year, here are some examples.

index.js
function getFirstDayOfYear(year) { return new Date(year, 0, 1); } // ๐Ÿ‘‡๏ธ Current Year const currentYear = new Date().getFullYear(); // ๐Ÿ‘‡๏ธ Sat Jan 01 2022 console.log(getFirstDayOfYear(currentYear)); console.log(getFirstDayOfYear(2027)); // ๐Ÿ‘‰๏ธ Fri Jan 01 2027 console.log(getFirstDayOfYear(2030)); // ๐Ÿ‘‰๏ธ Tue Jan 01 2030 console.log(getFirstDayOfYear(2035)); // ๐Ÿ‘‰๏ธ Mon Jan 01 2035

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, you can extract the month in a variable:

index.js
function getFirstDayOfYear(year) { const january = 0; return new Date(year, january, 1); }

Further Reading #

Join my newsletter

I'll send you 1 email a week with links to all of the articles I've written that week

Buy Me A Coffee