【问题标题】:Calculating hours difference between two dates in AngularJS [duplicate]计算AngularJS中两个日期之间的小时差[重复]
【发布时间】:2014-04-14 12:06:00
【问题描述】:

我有一个看起来像这样的模板:

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

我想计算task.time_stoptask.time_start 之间的小时差。无论如何,在模板中这样做"live"

【问题讨论】:

标签: javascript angularjs date


【解决方案1】:

为此使用 Moment 库:

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

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

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

或者甚至更好地为此创建一个工厂...以使其可重用

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

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

【讨论】:

    猜你喜欢
    • 2021-10-09
    • 1970-01-01
    • 2017-11-16
    • 1970-01-01
    • 1970-01-01
    • 2015-06-19
    • 1970-01-01
    • 2014-08-23
    相关资源
    最近更新 更多