Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

joshblevins's avatar

Check to see if any date field is less than the last day of the month

I have a form with expiration dates of about 30 medications. Is it possible in java script to check all date fields on a given page and alert the user prior to submission of the form that a medication is going to expire before the end of the month?

0 likes
1 reply
neilstee's avatar
neilstee
Best Answer
Level 34

@joshblevins something like this:

var expirationDates  = [new Date("2022-01-01"), new Date("2021-04-16")]; // your dates
var date = new Date(), y = date.getFullYear(), m = date.getMonth();
var lastDay = new Date(y, m + 1, 0);

for (i = 0; i < expirationDates.length; i++) {
	if (expirationDates[i] < lastDay){
   		alert('about to get expired!');
	}
}

Please or to participate in this conversation.