How to validate date if is the last day of the month with javascript?

Viewed 18628

How to validate user input date is the last day of the month using javascript?

6 Answers
function isLastDayOfMonth(date){
    return date.getDate() == new Date(date.getFullYear(),date.getMonth()+1,0).getDate();
}

If you want to avoid reinventing the wheel and having extra code to maintain and test, I highly suggest to use an external library like Moment.js or date-fns.

I personally advocate for date-fns as it is not as bulky as Moment and relies on Date primitives rather than Moment objects.

Your function of interest is named isLastDayOfMonth and can be found here

Related