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.
Copied!
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:
- The year
- The month
- 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.
Copied!
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.
Copied!
function getFirstDayOfMonth[year, month] { return new Date[year, month, 1]; } const january = 0; console.log[getFirstDayOfMonth[2026, january]]; // 👉️ Thu Jan 01 2026
Further Reading #
- Get the Last Day of a Month in JavaScript
- Get the Last Day of the Year in JavaScript
Very simple, no library required:
var date = new Date[];
var firstDay = new Date[date.getFullYear[], date.getMonth[], 1];
var lastDay = new Date[date.getFullYear[], date.getMonth[] + 1, 0];
or you might prefer:
var date = new Date[], y = date.getFullYear[], m = date.getMonth[];
var firstDay = new Date[y, m, 1];
var lastDay = new Date[y, m + 1, 0];
EDIT
Some browsers will treat two digit years as being in the 20th century, so that:
new Date[14, 0, 1];
gives 1 January, 1914. To avoid that, create a Date then set its values using setFullYear:
var date = new Date[];
date.setFullYear[14, 0, 1]; // 1 January, 14