Get date of last day of current month for special processing like for credit card expiration dates

In case you ever need to manually validate those pesky multi-field credit card date fields, you know “Month” and “Year” and you want to make sure the expiration is never set prior to the current month for obvious reasons like the credit card would get declined, you need to have the browser client side figure out (or be given the value through PHP injection into your JS script) the last date of the last day of the current month so you can then calculate and compare the “Month” and “Year” fields to this date value.

Using Javascript’s Date object should be adequate for most browsers and IE 8 or greater (there’s a work around to accommodate older browsers, but is it really worth reaching people who are using IE7?!  That’s a cost/benefit decision you and your marketing team will have to make.)

dt=new Date();
new Date(dt.getFullYear(), dt.getMonth() + 1, 0, 23, 59, 59)

No Javascript libraries like JQuery is required!

About Author:

Senior Cloud Software Engineer and 25+ years experienced video production, video editing and 3D animation services for a variety of global clients including local video production here in Jacksonville, Florida.

Leave a Comment

Your email address will not be published. Required fields are marked *