I wrote some JS to try and come up with with the last date of the month a user selects, I am trying to figure out why it is coming up with dates that dont exist, such as 6/31/2015, the problem is probably around how I am calculating end of month, but I just cant see my way around to the right conclusion here. Its an interesting bug so I thought I'd see if you guys have a solution.
var ealiest_effective_date = new Date(Date.parse("#{ruby_earliest_time_which_is_confirmed_correct.try(:strftime, "%m/%d/%Y")}"));
var minimumtime = Date.parse(new Date(ealiest_effective_date.getFullYear(), ealiest_effective_date.getMonth() + 2, 0));
$('.selected-date').change(function (e) {
date = Date.parse($(this).val());
user_selected_date = new Date(date);
console.log(user_selected_date.getMonth()+'/'+user_selected_date.getDate()+'/'+user_selected_date.getFullYear());
var possibleResultantEndOfMonth = Date.parse(new Date(user_selected_date.getFullYear(), user_selected_date.getMonth() + 2, 0));
var terminationEndOfMonth = new Date(Math.max(possibleResultantEndOfMonth,minimumtime));
var end_of_month_date = terminationEndOfMonth.getMonth()+'/'+terminationEndOfMonth.getDate()+'/'+terminationEndOfMonth.getFullYear();
$('#should_be_end_of_month').val(effective_date);
});