【问题标题】:how do i get the time difference in angular js? [duplicate]我如何获得角度js的时差? [复制]
【发布时间】:2017-11-22 14:12:42
【问题描述】:

我想根据外出时间和时间的差异计算 ot 小时。我想知道使用角度 js 实现它的可能方法。重要的是我需要它以小时:分钟:秒格式。以下是示例我的代码。

我的看法

<table class="row-border hover table table-bordered cb-data-table">
    <tr>
        <th>In Time</th>
        <th>Out Time</th>
        <th>OT Hours</th>
    </tr>

    <tr ng-show="!dailyAttendances.length">
        <td colspan="3" style="text-align: center">~ No Any Records ~</td>
    </tr>

    <tr  ng-repeat="dailyAttendance in dailyAttendances" >
          <td>@{{ dailyAttendance.in_time}}</td>
          <td>@{{ dailyAttendance.out_time}}</td>
          <td></td>                           
    </tr>
</table>

我的 javascript 文件

$scope.loadAllDailyAttendance = function () {

        $scope.dailyAttendances = {};


        $http({
            method: "GET",
            url: "/attendance/loadAllDailyAttendance",
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            },

        }).then(function successCallback(response) {
            $scope.dailyAttendances = response.data;
            console.log(response.data);
            }, function errorCallback(response) {
        });
    };

【问题讨论】:

  • 你怎么知道是上午还是下午?
  • {{dailyAttendance.out_time - regular_office_end_time}}
  • @shihas 它不起作用我已经尝试过了。@ramesh in_time 始终是 AM。

标签: javascript php angularjs laravel


【解决方案1】:

在组件中:

timeDuration(endTime: string, startTime: string): string {
    const endDate = new Date(endTime).getTime();
    const startDate = new Date(startTime).getTime();
    const time = endDate - startDate;
    return this.mlsecondsToTime(time);
}

private mlsecondsToTime(milliSec: number): string {
    function pad(n: number): string {
        const z = 2;
        return ('00' + n).slice(-z);
    }

    let x = milliSec / 1000;
    const seconds = x % 60;
    x /= 60;
    const minutes = Math.floor(x % 60);
    x /= 60;
    const hours = Math.floor(x % 24);
    return pad(hours) + ':' + pad(minutes) + ':' + pad(seconds);
}

在标记中:

  <td> {{timeDuration(dailyAttendance.in_time, dailyAttendance.in_time)}} </td>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-03
    • 2018-05-15
    • 2018-02-03
    • 1970-01-01
    相关资源
    最近更新 更多