Answer the question
In order to leave comments, you need to log in
I don’t understand why the code doesn’t work properly in js, who fumbles with the dates?
Hello, help me, I did a task where you need to write a function that will show the last day of the month, I already looked at the answer that it can be written in a couple of lines using autocorrect, but still, I would like to figure it out.
function getLastDayOfMonth(year, month) {
let date = new Date(year, month , 0);
for(let i = 1; i <= 31; i++) {
date.setDate(i);
if (date.getDate() !== i ) return i -1 ;
}
}
alert( getLastDayOfMonth(2012, 0) );
For some reason, at the 4th month, it just starts to return undefined, although I don’t interact with the month in any way, except for the transfer
Answer the question
In order to leave comments, you need to log in
function getMonthLastDay(year, month) {
return (new Date(year, month + 1, 0)).getDate();
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question