如何过滤格式化日期

时间:2016-08-02 20:27:59

标签: javascript angularjs filter

我从网络服务获取数据,即使日期为Date,翻译时类型也会丢失。我用过滤器解决了显示部分。但现在搜索需要调整。

现在我无法搜索2016-xx,当然一年中有超过4个日期2016

enter image description here

VIEW:使用过滤器将字符串转换为日期并格式化

                 <!--  at-attribute show DateTime unformated value -->
<td at-implicit at-attribute="DateTime"
    at-title="Datetime" at-sortable at-initial-sorting="desc">
    {{item.DateTime | mydate | date: 'yyyy-MM-dd HH:mm' }}                 
</td>
    <!--  mydate filter convert to Date and format -->

我如何更改过滤器?

控制器:这会更新要在表格中显示的列表。

$scope.globalFilter = function () {
   $scope.filteredList = $filter("filter")($scope.cars, $scope.filter_global);
}

自定义过滤器

app5.filter("mydate", function () {
    var re = /\/Date\(([0-9]*)\)\//;
    return function (x) {
        if (x !== null) {
            var m = x.match(re);
            if (m) return new Date(parseInt(m[1]));
            else return null;
        } else return null;
    };
});

示例数组:

   $scope.cars  = [
     {"Car_ID":2130,"X":121.01920100,"Y":14.51724100,"RoadName":null,"DateTime":"\/Date(1470231481000)\/"},
     {"Car_ID":1490,"X":121.10205000,"Y":14.25779300,"RoadName":"Eton Service Rd.","DateTime":"\/Date(1466735876000)\/"},
     {"Car_ID":2662,"X":121.03186700,"Y":14.49113200,"RoadName":null,"DateTime":"\/Date(1470226249000)\/"},
     {"Car_ID":2690,"X":121.03701700,"Y":14.61565000,"RoadName":"Dona Hemady","DateTime":"\/Date(1470122272000)\/"},
     {"Car_ID":2515,"X":121.02795800,"Y":14.56134900,"RoadName":"Makati Ave.","DateTime":"\/Date(1463770201000)\/"}
    ];

0 个答案:

没有答案