Get the First Day of a Month in JavaScript

avatar

Borislav Hadzhiev

Mon Oct 25 20212 min read

banner

Photo by Oliver Blum

Get the First Day of a Month #

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

index.js
function getFirstDayOfMonth(year, month) { return new Date(year, month, 1); } // ๐Ÿ‘‡๏ธ First day of CURRENT MONTH const date = new Date(); const firstDayCurrentMonth = getFirstDayOfMonth( date.getFullYear(), date.getMonth(), ); console.log(firstDayCurrentMonth); // ๐Ÿ‘‰๏ธ Sat Oct 01 2022 // ๐Ÿ‘‡๏ธ First day of month January 2025 const firstDayJanuary = getFirstDayOfMonth(2025, 0); console.log(firstDayJanuary); // 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.

We used the Date.getMonth method to get the current month.

The method returns an integer from 0 (January) to 11 (December).

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

We hardcoded 1 for the day, because we want the first day of the month.

The getFirstDayOfMonth function can be used to get the first day of any month, here are some examples.

index.js
function getFirstDayOfMonth(year, month) { return new Date(year, month, 1); } console.log(getFirstDayOfMonth(2026, 0)); // ๐Ÿ‘‰๏ธ Thu Jan 01 2026 console.log(getFirstDayOfMonth(2031, 0)); // ๐Ÿ‘‰๏ธ Wed Jan 01 2031 console.log(getFirstDayOfMonth(2036, 0)); // ๐Ÿ‘‰๏ธ Tue Jan 01 2036

The tricky thing to remember is that months are zero-based and go from 0 (January) to 11 (December).

This gets handled when you use the getMonth method as it returns an integer from 0 to 11.

If you want to make your code more readable for other months you can extract the value into a variable.

index.js
function getFirstDayOfMonth(year, month) { return new Date(year, month, 1); } const january = 0; console.log(getFirstDayOfMonth(2026, january)); // ๐Ÿ‘‰๏ธ Thu Jan 01 2026

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