I'm doing calculations on the end date of a membership, should be a no brainer but I think I'm expecting PHP logic to work in javascript. http://jsfiddle.net/q66qc/4/ I've created a fiddle to avoid having to paste screeds of code here. If you select a month and year the field under it will be populated with the date a year from now but if I select January instead of getting New Years Eve (Dec 31) I get this var newEnddate = new Date(startDate.getFullYear(), startDate.getMonth()+12, startDate.getDate()); newEnddate.setDate(newEnddate.getDate()-1); $('#FinanceEnddate').val(newEnddate.getFullYear() + "-" + ('00'+newEnddate.getMonth()).substr(-2,2) + "-" + newEnddate.getDate()); Code (markup):
A couple things going on here... the month functions are 0 based... January is 0, February is 1 and so on as far as the JavaScript Date() class. This is what you want I believe: function setCardExpiry() { var startDate = new Date($('#FinanceStartdateYear').val(), $('#FinanceStartdateMonth').val() - 1, 1); var newEnddate = new Date(startDate.getFullYear(), startDate.getMonth() + 12, startDate.getDate()); newEnddate.setDate(newEnddate.getDate() - 1); $('#enddate').val(newEnddate.getFullYear() + "-" + ('00' + (newEnddate.getMonth() + 1)).substr(-2, 2) + "-" + newEnddate.getDate()); } Code (javascript):