Encodera

Thanks for reading!

Donate to the site — we work hard for you.

How to get the first and last day of a month

Short answer

The first day is day 1. Day 0 of the next month is the last day of the current one:

const year = 2025;
const month = 1;
 
const first = new Date(year, month, 1);
const last = new Date(year, month + 1, 0);
 
console.log(first.toDateString());
console.log(last.toDateString());

Output:

Sat Feb 01 2025
Fri Feb 28 2025

The number of days

The same trick gives the number of days in a month:

const daysInMonth = (year, month) => new Date(year, month + 1, 0).getDate();
 
console.log(daysInMonth(2024, 1), daysInMonth(2025, 1), daysInMonth(2025, 11));

Output:

29 28 31

For the current month

Take the year and the month from today's date:

const today = new Date();
const first = new Date(today.getFullYear(), today.getMonth(), 1);
 
console.log(first.getDate());

Output:

1

Common mistake

The month in the constructor starts from zero. new Date(2025, 12, 0) is not an error: month 12 is January of the next year, and day 0 of it is December 31:

console.log(new Date(2025, 12, 0).toDateString());

Output:

Wed Dec 31 2025

Functions used in this recipe

Thanks for reading!

Donate to the site — we work hard for you.