Thursday 2 April 2020

Calculate number of months between two dates using javascript




<script>

    var dtfrm ='10/01/2019';//////////dd/mm/yyyy
    var dtto = '31/03/2019';
    var d1 = dtfrm.split('/');
    var d2 = dtto.split('/');
    var frstdate = new Date(d1[2], (parseFloat(d1[1]) - 1).toFixed(0), d1[0]);
    var secdate = new Date(d2[2], (parseFloat(d2[1]) - 1).toFixed(0), d2[0]);
    if (frstdate > secdate) {
        var dtothr;
        dtothr = frstdate;
        frstdate = secdate;       
        secdate = dtothr;
    }
    var mnth = ((secdate.getFullYear() * 12 + secdate.getMonth()) - (frstdate.getFullYear() * 12 + frstdate.getMonth()));

  
    alert(mnth);
</script>



Result///

No comments:

Post a Comment

Excel Sort values in ascending order using function TEXTJOIN

 Excel ::  Text ::  1,3,5,2,9,5,11 Result :: 1,2,3,5,5,9,11 Formula ::     TEXTJOIN ( ",",1,SORT(MID(SUBSTITUTE( A1 ,","...