Search This Blog

Sunday, October 13, 2019

Get No Of days between two Date from and To using Jquery

<!DOCTYPE html>
<html>
<title>Web Page Design</title>
<head>
<script>
var monthNames = [ "January", "February", "March", "April", "May", "June",
        "July", "August", "September", "October", "November", "December" ];
        var daysOfMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
       
        var sum=0;
        var All=100;

function diff(from, to) {
    var arr = [];
    var datFrom = new Date(from);
    console.log(datFrom);
    var datTo = new Date(to);
    console.log(datTo);
    var fromMonth=datFrom.getMonth();
    var fromYear =  datFrom.getFullYear();
    var toMonth=datTo.getMonth();
    var toYear =  datTo.getFullYear();
    if((fromMonth==toMonth) && (fromYear==toYear))
    {
        console.log('yes');
        var diffTime = Math.abs(datFrom - datTo);
        var diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
        console.log(diffDays);
        console.log(monthNames[toMonth]);
        console.log(daysOfMonth[toMonth]);
        sum +=(All*diffDays)/(daysOfMonth[toMonth]);
        console.log(sum);
    }else
    {
        console.log('No');
        var diffYear = (12 * (toYear - fromYear)) + datTo.getMonth();

    for (var i = datFrom.getMonth(); i <= diffYear; i++) {
        arr.push(daysOfMonth[i % 12]+ " " + [i%12] + " " + Math.floor(fromYear+(i/12)));
       // console.log(i);
        if(i == datFrom.getMonth() || i >= diffYear )
        {
            //console.log(daysOfMonth[i % 12]);
        }else
        {
            console.log(monthNames[i]);
            console.log(daysOfMonth[i % 12]);
            console.log('-----');
            sum +=(All*daysOfMonth[i % 12])/(daysOfMonth[i % 12]);
        }
       
       
    }
    if(arr.length>=2)
    {
       // console.log(arr[0]);
          var dateFromFormat=(parseInt(arr[0].split(' ')[1]) + 1)+ ' '+arr[0].split(' ')[0] + ' ' + arr[0].split(' ')[2];
       
        var date2From = new Date(dateFromFormat);
       // console.log(datFrom.getMonth());
        // console.log(date2From.getMonth());
       
        var diffTimeFrom = Math.abs(date2From - datFrom);
        var diffDaysFrom = Math.ceil(diffTimeFrom / (1000 * 60 * 60 * 24));
        console.log(monthNames[parseInt(arr[0].split(' ')[1])]);
        console.log(daysOfMonth[parseInt(arr[0].split(' ')[1])]);
        console.log(diffDaysFrom);
        console.log('-----');
        sum +=(All*diffDaysFrom)/(daysOfMonth[parseInt(arr[0].split(' ')[1])]);
       // console.log(arr[arr.length -1]);
        var dateToFormat=(parseInt(arr[arr.length -1].split(' ')[1]))+ ' '+arr[arr.length -1].split(' ')[0] + ' ' + arr[arr.length -1].split(' ')[2];
       //  console.log(dateToFormat);
        var date2To = new Date(dateToFormat);
       // console.log(datTo);
        var diffTimeTo = Math.abs(date2To - datTo);
        var diffDaysTo = Math.ceil(diffTimeTo / (1000 * 60 * 60 * 24));
        console.log(monthNames[parseInt(arr[arr.length -1].split(' ')[1])]);
        console.log(daysOfMonth[parseInt(arr[arr.length -1].split(' ')[1])]);
        console.log(diffDaysTo);
        console.log('-----');
        sum +=(All*diffDaysTo)/(daysOfMonth[parseInt(arr[arr.length -1].split(' ')[1])]);
    }
    console.log(sum);
    //console.log(arr[0].split(' ')[0]);
    return arr;
    }
   
   
}

console.log(diff('05 12 2013', '07 28 2013'));
</script>
</head>
<body>
</body>
</html>

No comments:

Post a Comment