Is there a way to calculate the number of months between two dates taking into account specific conditions, such as when the dates are not exact and may have different day counts? Here is an example using the moment library:
var date1 = moment('2021-05-30');
var date2 = moment('2021-06-30');
var result = date2.diff(date1, 'months');
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
In some cases, like when the dates are not standard month boundaries, such as '2021-06-1' to '2021-07-15'
, how can we tweak the calculation to get accurate results? Your insights or suggestions, whether with moment or another library, would be greatly appreciated. Thank you!