Reputation: 81
Choose 16-Jun-2019
and 16-May-2019
to calculate month difference; the answer is 0
, but it should be 1
.
If the first date is 16-May-19 and the other date from 16-June-19 up to (but not including) 16-July-19, then the result should be 1.
$("#lastAssimilationDate").datepicker({
yearRange: "-20:+100",
changeMonth: true,
changeYear: true,
dateFormat: "d-M-y"
});
$("#lastAssimilationDateOver").datepicker({
yearRange: "-20:+100",
changeMonth: true,
changeYear: true,
dateFormat: "d-M-y"
});
function monthDiff(d1, d2) {
var months;
months = (d2.getFullYear() - d1.getFullYear()) * 12;
months -= d1.getMonth() + 1;
months += d2.getMonth();
return months <= 0 ? 0 : months;
}
$('button').click(function() {
d1 = new Date($("#lastAssimilationDate").val());
d2 = new Date($("#lastAssimilationDateOver").val());
alert(monthDiff(d1, d2));
});
<script type="text/javascript" src="//code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript" src="//code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
<link rel="stylesheet" type="text/css" href="//code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css">
<input type='text' id="lastAssimilationDate" />
<input type='text' id="lastAssimilationDateOver" />
<button>press</button>
http://jsfiddle.net/praveen_jegan/mB6B7/1/
Upvotes: 0
Views: 68
Reputation: 6089
So you want the absolute difference in months.
First of all, don't add 1 to the month, i.e. replace months -= d1.getMonth() + 1;
by months -= d1.getMonth();
In the example, d1.getMonth() will be 5 and d2.getMonth() will be 4. The difference is -1.
If you want the absolute difference, replace return months <= 0 ? 0 : months;
by return months <= 0 ? -months : months;
In the example, the result will be 1.
The result so far:
function monthDiff(d1, d2) {
var months;
months = (d2.getFullYear() - d1.getFullYear()) * 12;
months -= d1.getMonth();
months += d2.getMonth();
return months <= 0 ? -months : months;
}
But this does not take the day number into account.
If you want to take day numbers into account, you can use getDate()
. The only case that the abovementioned function gives an incorrect result, is when the day component of the earlier date is greater than the day component of the later date. In that case, we need to subtract 1 from months.
function monthDiff(d1, d2) {
var months;
if (d1 > d2) {
months = monthDiff(d2,d1);
} else {
months = (d2.getFullYear() - d1.getFullYear()) * 12;
months += d2.getMonth() - d1.getMonth();
if (d1.getDate() > d2.getDate()) {
months--;
}
}
return months;
}
Upvotes: 3
Reputation: 53
I have check on your code. It seems there is bug there. Let see this code:
function monthDiff(d1, d2) {
var months;
months = (d2.getFullYear() - d1.getFullYear()) * 12;
months -= d1.getMonth() + 1;
months += d2.getMonth();
return months <= 0 ? 0 : months;
}
The bug is happen when you substract months with d1.getMonth + 1.
months -= d1.getMonth() + 1;
You can change your code to be like this:
function monthDiff(d1, d2) {
var months;
months = (d2.getFullYear() - d1.getFullYear()) * 12;
months = months - d1.getMonth() + d2.getMonth();
return months <= 0 ? 0 : months;
}
Then try to input 17-May-2019 as lastAssimilationDate input and 16-Jun-2019 as lastAssimilationDateOver.
Upvotes: 0