1

In my application displaying the users age based on their date of birth

controller

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

view

  {{currentIDate - dob}} -- 300297  

How to remove last 4 digest using filter to get users age?

here is the code :

// 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="angular.js@1.2.22" 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>
Lrrr
  • 4,755
  • 5
  • 41
  • 63
prasannaboga
  • 1,004
  • 1
  • 14
  • 36

1 Answers1

1

use this it will work :

function simpleController($scope, $filter) {
  $scope.currentIDate = $filter('date')(new Date(), 'yyyyMMdd');
  $scope.dob = 19840623
}
<!DOCTYPE html>
<html ng-app=''>
  <head>
    <script data-require="angular.js@1.2.22" 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>
Lrrr
  • 4,755
  • 5
  • 41
  • 63