1

I am trying to calculate experience of an employee in my application. I was planning to do so by subtracting 'careerStartedOn' from the 'Current date'.

Here's my code for controller :

myApp.controller('getAllBenchersController', ['$scope', 'employeeTalentPoolServices', 'dataTable', '$window', '$timeout', function ($scope, employeeTalentPoolServices, dataTable, $window, $timeout) {
    employeeTalentPoolServices.getAllBenchers().then(function (result) {
     var mainData = result.data;
        $scope.date = new Date();
         $scope.blockEmployee = function (id) {

            employeeTalentPoolServices.blockEmployee(id);
            $scope.showhide = false;
        }
         });

    employeeTalentPoolServices.getCustomerAccounts().then(function (result) {
        $scope.accountData = result.data;
        });

}]);

Html :

<div class="widget-content table-container" ng-controller="getAllBenchersController">
<table ng-table="talentPoolList" show-filter="true" class="table table-striped table-bordered">
                    <tr ng-repeat="employee in data">
      <td data-title="'Experience'" sortable="'Experience'" filter="{ 'account': 'text' }">
                         {{employee.careerStartedOn | date:myApp.dateFormat}}
                        </td>
    </tr>
                </table>

in HTML, if i call {{employee.careerStartedOn | date:myApp.dateFormat}} i will get the 'careerStartedOn' date and when i call {{date | date:myApp.dateFormat}} i will get the current date.

I need to substract the careerStartedOn date from the current date and then display it in the <td>. I am a fresher and cant find a solution to do this.

Can anyone help me out with the code to achieve this?

Thanks in advance...

Atal Kishore
  • 4,480
  • 3
  • 18
  • 27
Phoenix
  • 285
  • 9
  • 28
  • Possible duplicate: http://stackoverflow.com/questions/4944750/how-to-subtract-date-time-in-javascript – IronAces Oct 14 '16 at 06:52
  • what you want to display? and you can use `moment.js` for this – xkeshav Oct 14 '16 at 06:52
  • I want to display the result of the calulation (CurrentDate - CareerStartedOn . The difference should be in no of years/months – Phoenix Oct 14 '16 at 06:54
  • @DanielShillcock - Its using javascript in the post you mentioned. I want to know how to achieve that in AngularJS with the controller. – Phoenix Oct 14 '16 at 06:56
  • @pro.mean - I want to display the result of the calulation (CurrentDate - CareerStartedOn . The difference should be in no of years/months. Can you show me an example of how to achieve this using moment.js? I am not sure where to do it. In the controller or in HTML? – Phoenix Oct 14 '16 at 06:59
  • See [this](http://stackoverflow.com/questions/26649194/calculating-days-difference-with-using-angular-and-jquery-datepicker). This may be useful to you. – Nishant123 Oct 14 '16 at 07:14
  • @Nishant123 - In the example, they are reading values using date time picker. What I want is to fetch the date from API and substract it from current date and display it in number of years/ months(not in number of days). – Phoenix Oct 14 '16 at 07:59
  • @Phoenix Can you please share your date format – Nishant123 Oct 14 '16 at 08:15
  • @Phoenix I have added an answer. Please see if that helps you – Nishant123 Oct 14 '16 at 08:46
  • I will check on that now @Nishant123.... My date format on 'careerStartedOn' is '2016-10-03T13:44:00.587' – Phoenix Oct 14 '16 at 08:57

2 Answers2

2

You can use this function to calculate experience in years/months

$scope.CalDate = function(date1,date2) {
    var diff = Math.floor(date1.getTime() - date2.getTime());
    var secs = Math.floor(diff/1000);
    var mins = Math.floor(secs/60);
    var hours = Math.floor(mins/60);
    var days = Math.floor(hours/24);
    var months = Math.floor(days/31);
    var years = Math.floor(months/12);
    months=Math.floor(months%12);
    days = Math.floor(days%31);
    hours = Math.floor(hours%24);
    mins = Math.floor(mins%60);
    secs = Math.floor(secs%60); 
    var message = ""; 
    if(days<=0){
    message += secs + " sec "; 
    message += mins + " min "; 
    message += hours + " hours "; 
    }else{
            if(years>0){
            message += years + " years ";    
        }
        if(months>0 || years>0){
            message += months + " months ";
        }
        message += days + " days";
    }
    return message
};


$scope.getExp = function(date)
{
    date = new Date($filter('date')(date, "yyyy/MM/dd"));
  var currdate = new Date($filter('date')(new Date(), "yyyy/MM/dd"));

  var exp = $scope.CalDate(currdate,date);
  return exp;
}

HTML

<table ng-table="talentPoolList" show-filter="true" class="table table-striped table-bordered">
 <th>
   <tr>
     <td>Career Started On</td>
     <td align="center">Experience</td>
   </tr>
 </th>
 <tr ng-repeat="employee in data">
  <td>
     {{employee.careerStartedOn | date: 'yyyy/MM/dd'}}
   </td>
   <td align="center">
   {{getExp(employee.careerStartedOn)}}
   </td>
</tr>
</table>

If you don't need days to be displayed you can remove this line

message += days + " days";

FULL EXAMPLE

Source

Community
  • 1
  • 1
Nishant123
  • 1,968
  • 2
  • 26
  • 44
  • My date format in 'careerStartedOn' is '2016-10-03T13:44:00.587'. Also you are hardcoding the date in the function. How can I take it from my API {{employee.careerStartedOn}} – Phoenix Oct 14 '16 at 09:02
  • `date` filter formats `date object` into a `string`. You need to convert it back to `date object` by `new Date("YOUR STRING DATE")`. So whatever date you are getting from `employee.careerStartedOn` after applying `date filter` pass that formated date to `new Date("PASS IT HERE")`. And use the CalDate() to calculate experience – Nishant123 Oct 14 '16 at 09:16
  • Okay but as I am still in learning phase, I am not sure how to do it. Can you please show it in your answer? Tnx – Phoenix Oct 14 '16 at 09:20
  • @Phoenix I have updated my answer. I have taken a dummy `data array` with some values in it. Please see the updated link also – Nishant123 Oct 14 '16 at 09:43
  • $scope.getExp = function (date) { is getting the value (date = "2016-10-03T13:44:00.587). After that it is not executing the statement : var currdate = new Date($filter('date')(new Date(), "yyyy/MM/dd")); – Phoenix Oct 14 '16 at 09:59
  • Add `'$filter'` service to your controller – Nishant123 Oct 14 '16 at 10:01
1

You can do something like below in your HTML to display experience in terms of number of years

           <tr ng-repeat="employee in data">
  <td data-title="'Experience'" sortable="'Experience'" filter="{ 'account': 'text' }">
                     {{getExp(employee.careerStartedOn)}}
                    </td>

In your JS

    $scope.getExp = function(str) {
    var today = new Date();
    var startDate = new Date(str);
    var exp = today.getFullYear() - startDate.getFullYear();
    var m = today.getMonth() - startDate.getMonth();
    return exp;
  };
ngCoder
  • 2,095
  • 1
  • 13
  • 22
  • https://s18.postimg.org/3l50bk8nt/debug.jpg I tried that, but it does not execute the var diff= date.getTime() - startDate.getTime(); Please check screenshot for info – Phoenix Oct 14 '16 at 08:55
  • you are sending a date string into the function instead try sending date object – ngCoder Oct 14 '16 at 08:58
  • The error is :: "[$interpolate:interr] Can't interpolate: {{getExp(employee.careerStartedOn)}} TypeError: startDate.getTime is not a function – Phoenix Oct 14 '16 at 09:15
  • @Phoenix I have updated the answer try now ! The error was because you are sending date as string object instead of date that is why there is no getTime () function . – ngCoder Oct 14 '16 at 09:25
  • Ok, I want the result to be converted to years/months/days to make it an exact amount. with this code days wont be calculated? – Phoenix Oct 14 '16 at 09:46
  • you can use the below answer by @Nishant for that just changing the function logic – ngCoder Oct 14 '16 at 09:49
  • Thanks a lot :) appreciate your time and effort. – Phoenix Oct 14 '16 at 10:08