Currently working on jQuery datepicker and jquery clone with my current code where i can able to add years for all the rows but when i am trying to add the months for example from date - 01/01/2000 to date 12/01/2000 it the message should come as 0 years and 11 months but currently it is coming as 0 years and 0 months it was not calculating the months
here is the code for months
function monthDiff(d1, d2) {
var months;
months = (d2.getFullYear() - d1.getFullYear()) * 12;
months -= d1.getMonth();
months += d2.getMonth();
return months <= 0 ? 0 : months;
}
and i am calling the months here
if ((firstDate !== "") && (lastDate !== "") && (!dateRegex.test(firstDate)) && (!dateRegex.test(lastDate))) {
firstDate = new Date(firstDate[2], firstDate[0] - 1, firstDate[1]);
lastDate = new Date(lastDate[2], lastDate[0] - 1, lastDate[1]);
diffMonths1 = monthDiff(firstDate, lastDate);
diffYears1 = diffMonths1 / 12;
diffYears += parseInt('' + diffYears1);
diffMonths += diffMonths1 - (diffYears1 * 12);
document.getElementById("txt_expy").innerHTML = diffYears;
document.getElementById("txt_expm").innerHTML = diffMonths;
}
And when i am trying to delete the row it should delete the row and corresponding year and month should remove.
Here is what i am trying for remove the row
$(document).on('click', ".btn_less1", function() {
var len = $('.cloned-row3').length;
if (len > 1) {
//$(this).closest(".btn_less1").parent().parent().parent().remove();
var RemoveStartDate = $(this).closest(".btn_less1").parent().parent().parent().find('.startDate ').val();
var RemoveEndDate = $(this).closest(".btn_less1").parent().parent().parent().find('.endDate ').val();
if ((RemoveStartDate != '') || (RemoveEndDate != '')) {
var dateStartArray = RemoveStartDate.split('/'),
dateEndArray = RemoveEndDate.split('/');
var fromdate = new Date(dateStartArray[2], dateStartArray[0] - 1, dateStartArray[0]),
todate = new Date(dateEndArray[2], dateEndArray[0] - 1, dateEndArray[0]);
var yearsDifference = todate.getFullYear() - fromdate.getFullYear();
var monthsDifference = (todate.getMonth() + 12 * todate.getFullYear()) - (fromdate.getMonth() + 12 * fromdate.getFullYear());
var PrevTotalYear = parseInt(document.getElementById("txt_expy").innerHTML);
var PrevTotalMonth = parseInt(document.getElementById("txt_expm").innerHTML);
document.getElementById("txt_expm").value='';
document.getElementById("txt_expy").value='';
PrevTotalYear = PrevTotalYear * 12;
var CurTotalYear = Math.floor(((PrevTotalYear + PrevTotalMonth) - monthsDifference) / 12);
var CurTotalMonth = (monthsDifference - PrevTotalMonth) % 12;
$("#txt_expy>span").text(CurTotalYear);
$("#txt_expm>span").text(CurTotalMonth);
$(this).closest(".btn_less1").parent().parent().parent().remove();
} else {
$(this).closest(".btn_less1").parent().parent().parent().remove();
}
}
});
http://jsfiddle.net/karthic2914/7po8w3sw/3/
Kindly help me where i am doing wrong here