Jenny Mok Jenny Mok - 1 year ago 106
Javascript Question

setDate doesn't return the last day of the month Javascript

I expect 31 because this month October has 31 days, which part of my code is wrong here?

function lastDayOfMonth(given_month) {
var d = new Date();
return d.toISOString();



sb9 sb9
Answer Source

sets the date to the last day of the previous month. I.e if you run this script in October, the date will be set to Sep 30.

Afterwards you run


This sets the month, but leaves the day of month as it is. I assume that given_month is 9 (i.e. Oct), so this will result in Oct 30.

See for more info.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download