2014-09-20 41 views
1

在我的應用程序中顯示的用戶年齡根據他們的出生angularjs。減去值,並移除最後四位數字

控制器的日期

$scope.currentIDate = $filter('date')(new Date(), 'yyyyMMdd'); 
    $scope.dob = 19840623 # converted user age into yyyyMMdd format 

視圖

{{currentIDate - dob}} -- 300297 

如何刪除過去的4摘要使用過濾器來獲取用戶年齡?

這裏是代碼:

// Code goes here 
 

 
function simpleController($scope, $filter) { 
 
    $scope.currentIDate = $filter('date')(new Date(), 'yyyyMMdd'); 
 
    $scope.dob = 19840623 
 
}
<!DOCTYPE html> 
 
<html ng-app=''> 
 
    <head> 
 
    <script data-require="[email protected]" data-semver="1.2.22" src="https://code.angularjs.org/1.2.22/angular.js"></script> 
 
    <link rel="stylesheet" href="style.css" /> 
 
    <script src="script.js"></script> 
 
    </head> 
 

 
    <body> 
 
    <h1>Display Age</h1> 
 
    <div ng-controller='simpleController'> 
 
     Today: {{currentIDate}} <br> 
 
     DOB: {{dob}} <br> 
 
     Sub: {{currentIDate - dob}} <br> 
 
     Age: {{currentIDate - dob | limitTo:2}} <br> 
 
    </div> 
 
    </body> 
 

 
</html>

+0

你爲什麼不跟之前的 「YYYY」 模式篩選日期,而不是後期處理呢? – 2014-09-20 11:29:50

+1

檢查[this](http://stackoverflow.com/questions/4060004/calculate-age-in-javascript)。 – 2014-09-20 11:36:02

回答

1

使用這種它將工作:

function simpleController($scope, $filter) { 
 
    $scope.currentIDate = $filter('date')(new Date(), 'yyyyMMdd'); 
 
    $scope.dob = 19840623 
 
}
<!DOCTYPE html> 
 
<html ng-app=''> 
 
    <head> 
 
    <script data-require="[email protected]" data-semver="1.2.22" src="https://code.angularjs.org/1.2.22/angular.js"></script> 
 
    <link rel="stylesheet" href="style.css" /> 
 
    <script src="script.js"></script> 
 
    </head> 
 

 
    <body> 
 
    <h1>Display Age</h1> 
 
    <div ng-controller='simpleController'> 
 
     Today: {{currentIDate}} <br> 
 
     DOB: {{dob}} <br> 
 
     Sub: {{currentIDate - dob}} <br> 
 
     Age: {{((currentIDate - dob)/10000) | number:0 }} <br> 
 
    </div> 
 
    </body> 
 

 
</html>