在Angularjs列表中显示TimeSpan类型数据

时间:2016-03-24 06:58:40

标签: angularjs timespan listings

尝试在表格中显示时间,但它以这种格式显示我的时间

{"Hours":16,"Minutes":8,"Seconds":45,"Milliseconds":0,"Ticks":581250000000,"Days":0,"TotalDays":0.6727430555555556,"TotalHours":16.145833333333332,"TotalMilliseconds":58125000,"TotalMinutes":968.75,"TotalSeconds":58125}

虽然我希望它以这种格式显示

"12:13:20"

在Html文件中,我像这样列出

<table>
    <thead>
        <tr>
            <th>Start Time</th>
            <th>End Time</th>
        </tr>
    </thead>
    <tbody>
        <tr ng-repeat="item in Festivals">
            <td>{{item.StartTime}}</td>
            <td>{{item.EndTime}}</td>
        </tr>
    </tbody>
</table>

我正在初始化数据的角度控制器代码

var app = angular.module('myApp');

app.controller('SeasonCtrl', [
    '$scope', '$http', 'seasonService', function ($scope, $http, seasonService) {

        $scope.Seasons = [];

        seasonService.GetAllSeasons = function () {
            seasonService.getSeasons().success(function (data) {
                $scope.Seasons = data.data;
            })
        }

        seasonService.GetAllSeasons();

    }
]);

C#代码我将数据发送到角度控制器

public JsonResult GetAllSeasons()
{
    var data = _seasonManager.AllSeasons();
    if (data != null)
    {
        var list = data.Select(r => new
        {
            r.StartTime,
            r.EndTime
        });

        return Json(new { data = list }, JsonRequestBehavior.AllowGet);
    }
    return null;
}

3 个答案:

答案 0 :(得分:1)

我刚试过这个,它解决了我的问题

在Html中

<table>
    <thead>
        <tr>
            <th>Start Time</th>
            <th>End Time</th>
        </tr>
    </thead>
    <tbody>
        <tr ng-repeat="item in Festivals">
            <td ng-bind='timeFunction(item.StartTime)'></td>
<td ng-bind='timeFunction(item.EndTime)'></td>
        </tr>
    </tbody>
</table>

在角度控制器中

$scope.timeFunction = function (timeObj) {
      var min = timeObj.Minutes < 10 ? "0" + timeObj.Minutes : timeObj.Minutes;
      var sec = timeObj.Seconds < 10 ? "0" + timeObj.Seconds : timeObj.Seconds;
      var hour = timeObj.Hours < 10 ? "0" + timeObj.Hours : timeObj.Hours;
      return hour + ':' + min + ':' + sec;
};

答案 1 :(得分:0)

此解决方案使用过滤器和Moment.js。

在JS中:

angular.module('app')
    .filter('showTimeSpan', function () {
        return function (timeObj, format) {
            return moment(timeObj, "HH:mm").format(format);
        };
    });

在HTML中:

<p>{{activity.startTime | showTimeSpan:"hh:mm a"}}</p>

答案 2 :(得分:-1)

您可以使用Angular中的date过滤器来显示格式化的日期时间:

在HTML中:

<p>{{mydate | date:'H:mm:ss'}}</p>

在JS中:

$scope.mytime = $filter('date')(mydate, 'H:mm:ss');