2014-09-01 55 views

回答

0

請看這裏http://plnkr.co/edit/CeZrzGqD90cvaMLER6R5?p=preview 我已經做了消費,當年有365天。

angular.module('plunker', ['ui.bootstrap']); 
    var DatepickerDemoCtrl = function($scope) { 

     $scope.open = function($event, opened) { 
     $event.preventDefault(); 
     $event.stopPropagation(); 

     $scope[opened] = true; 
     }; 

     $scope.dateOptions = { 
     formatYear: 'yy', 
     startingDay: 1 
     }; 


function yearDiff(first, second) { 
    if(second && first){ 
    return (second-first)/(1000*60*60*24*365); 
    } 
    else 
    { 
    return 0; 

    } 
} 

    $scope.$watch('start', function(i){ 
    $scope.diff = yearDiff($scope.start, $scope.end) 
     console.log($scope.diff); 
    }); 
    $scope.$watch('end', function(i){ 
    $scope.diff = yearDiff($scope.start, $scope.end) 
     console.log($scope.diff); 
    }); 


     $scope.formats = ['dd-MMMM-yyyy', 'yyyy/MM/dd', 'dd.MM.yyyy', 'shortDate']; 
     $scope.format = $scope.formats[0]; 
    }; 
+0

感謝您的代碼!當我在我的代碼中實現相同的時候,第二個投擲會給出一個NaN! – lrd123 2014-09-03 13:01:18

+0

@ lrd123我更新了代碼,請參閱answear – sylwester 2014-09-03 13:07:55

+0

這種方式奏效!我的問題是我的日期是一個字符串。所以我做了一個新的日期(mystring),然後跟着你的代碼。謝謝 – lrd123 2014-09-03 14:45:42

相關問題