Calculating days difference with using angular and jQuery datepicker

后端 未结 2 939
天命终不由人
天命终不由人 2020-12-17 04:42

In angular, I am trying to calculate the number of days between two selected date with using jQuery datepicker. It returns only null with the function that I am using..

相关标签:
2条回答
  • 2020-12-17 05:14

    here is the working fiddle please check , link to fiddle

    create a function for get the dateString to pass into new Date()

    $scope.formatString = function(format) {
        var day   = parseInt(format.substring(0,2));
        var month  = parseInt(format.substring(3,5));
        var year   = parseInt(format.substring(6,10));
        var date = new Date(year, month-1, day);
        return date;
    }
    

    modify the dayDiff function as below,

    $scope.dayDiff = function(firstDate,secondDate){
        var date2 = new Date($scope.formatString(secondDate));
        var date1 = new Date($scope.formatString(firstDate));
        var timeDiff = Math.abs(date2.getTime() - date1.getTime());   
        var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24)); 
        alert(diffDays);
    }
    
    0 讨论(0)
  • 2020-12-17 05:21

    angular-moment does the trick! ...and (very) else more.

    Using the amDifference filter:

    Get the difference between two dates in milliseconds. Parameters are date, units and usePrecision. Date defaults to current date. Example:

    <span>Difference: {{ dateFrom | amDifference : dateTo : 'days' }} days</span>
    
    0 讨论(0)
提交回复
热议问题