Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the difference in months between two dates in (mm/dd/yyyy) format using jquery

I was wanting to retrieve the month differences between two dates that a user enters in using the bootstrap datepicker. Along with this I am also trying to replace the second value (date2) to what the difference in months is when the user clicks the calculate button. When trying to work on this my code does not seem to be working...

Here is what I have so far:

$('#clickMe').click(function() {
    var date1 = $('#firstPayDate');
    var date2 = $('#loanTrm');
    var timeDiff = Math.abs(date2.getTime() - date1.getTime());
    var diffMonths = Math.ceil(timeDiff / (1000 * 3600 * 24)); 
    date2.val(date2.val().replace(diffMonths));
});

Here is a picture demonstration of what I am trying to accomplish:

enter image description here

I am trying to take the difference between these two entries (Maturity Date - First payment date)

enter image description here I am trying to get the calculations to occur once the user clicks the Calculate button, the difference in months gets replaced with the value that was entered in Maturity Date.

Any help will be greatly appreciated!

like image 648
Alex111 Avatar asked Mar 14 '23 13:03

Alex111


1 Answers

Below logic will get difference in months

(endDate.getFullYear()*12+endDate.getMonth())-(startDate.getFullYear()*12+startDate.getMonth())
like image 162
Anoop Isaac Avatar answered Apr 24 '23 20:04

Anoop Isaac