Borislav Hadzhiev
Reading timeยท3 min
Photo from Unsplash
To get the number of days in a month:
new Date()
constructor, passing it 0
for the days.getDate()
method on the result to get the number of days in the
month.function getDaysInMonth(year, month) { return new Date(year, month, 0).getDate(); } const date = new Date(); const currentYear = date.getFullYear(); const currentMonth = date.getMonth() + 1; // ๐๏ธ months are 0-based // ๐๏ธ Current Month const daysInCurrentMonth = getDaysInMonth(currentYear, currentMonth); console.log(daysInCurrentMonth); // ๐๏ธ 31 // ๐๏ธ Other Months const daysInJanuary = getDaysInMonth(2025, 1); console.log(daysInJanuary); // ๐๏ธ 31 const daysInSeptember = getDaysInMonth(2025, 9); console.log(daysInSeptember); // ๐๏ธ 30
We passed the following 3 arguments to the new Date() constructor:
Date
objectDate
object. Month
indexes are zero-based where 0
is January and 11
is December.Date
object. When 0
is used for the days,
we get back the last day of the previous month. We use this approach to
balance out the zero-based month index.getMonth()
to the function, make sure to add 1
to the month index.const date = new Date(); // ๐๏ธ add 1 to get correct value const currentMonth = date.getMonth() + 1;
Specifying 0
for the days
parameter, allows us to use intuitive month
indexes when using the new Date()
constructor.
For example, passing a month index of 2
, gives us the last day in February,
and not March.
console.log(new Date(2025, 1, 0)); // ๐๏ธ Fri January 31 2025 console.log(new Date(2025, 2, 0)); // ๐๏ธ Fri Feb 28 2025
The last step is to call the Date.getDate method.
console.log(new Date(2025, 1, 0).getDate()); // ๐๏ธ 31 console.log(new Date(2025, 2, 0).getDate()); // ๐๏ธ 28
The method returns an integer from 1
to 31
, which represents the day of the
month for a given date according to local time.
Getting the integer representation of the last day of the month is the equivalent of getting the number of days in the month.
To get the number of days in the current month:
new Date()
constructor to get a date object that corresponds to the
last day of the current month.getDate()
method to get an integer representing the last day of
the month.function getDaysInCurrentMonth() { const date = new Date(); return new Date(date.getFullYear(), date.getMonth() + 1, 0).getDate(); } const result = getDaysInCurrentMonth(); console.log(result); // ๐๏ธ 31
We passed the following 3 arguments to the new Date() constructor:
0
is January
and 11
is December. This is why we added 1
to the result.0
is used as the day, we
get back the last day of the previous month. We use this approach to
balance out the zero-based month index.It's quite confusing but the getMonth()
method returns a zero-based index.
const date = new Date('January 04, 2025 05:24:07'); console.log(date.getMonth()); // ๐๏ธ 0
To balance this out, we passed 0
to the days
parameter of the new Date()
constructor to get the last day of the prior month.
For example, passing a month index of 2
, gives us the last day of February and
not March.
console.log(new Date(2025, 1, 0)); // ๐๏ธ Fri January 31 2025 console.log(new Date(2025, 2, 0)); // ๐๏ธ Fri Feb 28 2025
new Date()
constructor returns an object representing the last day of the current month.The last step is to call the Date.getDate method.
function getDaysInCurrentMonth() { const date = new Date(); return new Date(date.getFullYear(), date.getMonth() + 1, 0).getDate(); } const result = getDaysInCurrentMonth(); console.log(result); // ๐๏ธ 31
The method returns an integer (from 1
to 31
) that represents the day of the
month for a given date.
Getting the integer representation of the last day of the current month is the equivalent of getting the number of days in the month.