Calculating the difference of days using angular and jQuery datepicker

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

However, I'm not quite sure if this is a function or is using datepicker in angular .. It seems to be a problem in the function, but it works when I use it with jQuery ..

my function:

$scope.dayDiff = function(firstDate,secondDate){

    $scope.dayDifference = parseInt(Math.round((secondDate - firstDate) / (1000 * 60 * 60 * 24)));;
}

the rest: http://jsfiddle.net/1mzgLywe/5/

Thanks in advance!

+4
source share
2 answers

here is the working violin, please check the link to the violin

create a function to transition dateString to 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;
}

dayDiff, ,

$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);
}
+7

angular-moment !... () .

amDifference:

. : , usePrecision. . :

<span>Difference: {{ dateFrom | amDifference : dateTo : 'days' }} days</span>
+6

All Articles