计算AngularJS中两个日期之间的小时差异[重复]

时间:2021-01-11 21:33:26

This question already has an answer here:

这个问题在这里已有答案:

I've got template which looks like this:

我有模板,看起来像这样:

<tr ng-repeat="task in tasks" class="thumbnail">
            <td ng-model="task.id">{[{ task.id }]}</td>
            <td ng-model="task.time_start">{[{ task.time_start | date : 'MMM d, y HH:mm' }]}</td>
            <td ng-model="task.time_stop">{[{ task.time_stop | date : 'MMM d, y HH:mm' }]}</td>
            <td>[here i want the time difference]</td>
            <td><button ng-click="edit(task)">Update</button></td>
        </tr>

and I want to count hours difference between task.time_stop and task.time_start. Is there anyway to do this "live", in template?

我想计算task.time_stop和task.time_start之间的小时差异。无论如何,在模板中“活着”吗?

1 个解决方案

#1


5  

Use the Moment library for this:

使用Moment库:

moment.utc(moment(task.time_stop.diff(moment(task.time_start))).format("mm")

You can wrap this call in a function:

您可以在函数中包装此调用:

$scope.timediff = function(start, end){
  return moment.utc(moment(end).diff(moment(start))).format("mm")
}

or even better create a factory for this... to make it reusable

甚至更好地为此创建工厂......使其可重复使用

PS: update your code to call the $scope.timediff to display the time difference:

PS:更新您的代码以调用$ scope.timediff来显示时差:

<td>timediff(task.start,task.end )</td>

#1


5  

Use the Moment library for this:

使用Moment库:

moment.utc(moment(task.time_stop.diff(moment(task.time_start))).format("mm")

You can wrap this call in a function:

您可以在函数中包装此调用:

$scope.timediff = function(start, end){
  return moment.utc(moment(end).diff(moment(start))).format("mm")
}

or even better create a factory for this... to make it reusable

甚至更好地为此创建工厂......使其可重复使用

PS: update your code to call the $scope.timediff to display the time difference:

PS:更新您的代码以调用$ scope.timediff来显示时差:

<td>timediff(task.start,task.end )</td>