0

I'm trying to calculate the difference between two dates that i'm getting from a backend service. im getting a list that contains date in this format : "dateTimeZ":"2021-09-22T12:41:56.133Z" So what im trying to do is that i want to show a new column that contains the difference betwwen the date in the current item with the previous one :

<div class="listHistory">
<table class="table">
  <caption hidden>Actions List Table Description</caption>
  <th scope="col">Object Id</th>
  <th scope="col">Object</th>
  <th scope="col">Object Name</th>
  <th scope="col">Action</th>
  <th scope="col">Action Author</th>
  <th scope="col">Action Time</th>
  <th scope="col">Amount of Time between Actions</th>
<tr scope="row" *ngFor="let action of caseActions?.results; let i = index" class="rowHistory">
  <td>{{action.id}}</td>
  <td>{{action.type}}</td>
  <td>{{action.name}}</td>
  <td>{{action.action}}</td>
  <td>{{action.author}}</td>
  <td>{{action.dateTimeZ | dateFormatter}}</td>
  <td>{{amount(i)}}</td>
  </tr>
   </table>
  </div>

in the .ts file i added a function amount(i) that will do the calculation and return the difference: if i=0 then it will return 0 because it doesn't have a previous one but how to calculate the rest to get it the result in this format : 1d 3h 15m

  amount(i){
if(i==0){
  return 0;
} else{
????????????
  }
  }
SS_FStuck
  • 231
  • 1
  • 5
  • 20
  • 1
    See the duplicate links above. Angular is built on typescript which is a super set of javascript so really you need to do the calculation using javascript which is outside of anything specific to angular. Personally I would use moment.js but you can also use javascript Date. – Igor Sep 22 '21 at 13:06
  • 1
    @Igor I'm not so sure about the recommendation for momentjs anymore. Even its developers nowadays advise against using it for new projects, because it's considered deprecated for quite a while now. https://momentjs.com/docs/#/-project-status/ – derpirscher Sep 22 '21 at 13:56
  • Thanks for that @derpirscher, I was not aware of this. – Igor Sep 22 '21 at 14:11
  • Date-fns is a good, tree-shakable alternative to momentjs. – MikeOne Sep 22 '21 at 15:00

0 Answers0