Phoenix Phoenix - 8 days ago 4x
AngularJS Question

How to subtract a date from current date in AngularJS

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 =;
$ = new Date();
$scope.blockEmployee = function (id) {

$scope.showhide = false;

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


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}}

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
. 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...


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);
    days = Math.floor(days%31);
    hours = Math.floor(hours%24);
    mins = Math.floor(mins%60);
    secs = Math.floor(secs%60); 
    var message = ""; 
    message += secs + " sec "; 
    message += mins + " min "; 
    message += hours + " hours "; 
            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;


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

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

message += days + " days";