Get the First Day of a Month in JavaScript

avatar

Borislav Hadzhiev

Last updated: Jul 25, 2022

banner

Photo from Unsplash

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 arguments 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, extract the value for the month 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 #

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.