【问题标题】:Json String Date to date Object with AngularJS Http getJson String Date to date Object with AngularJS Http get
【发布时间】:2015-06-11 09:12:50
【问题描述】:

我正在尝试按日期和受欢迎程度排序列表。从服务中,我有一个 json 响应,其中包含字符串格式的日期。是否可以将日期从 Http 获取响应更改为日期对象,以便我想使用 OrderBy 过滤器按日期对列表进行排序。

我的json是

{
  "Name": "Paul ",
  "Country": "SINGAPORE",
  "Date": "12/31/14 20:40",
  "Rating": "2"
}

【问题讨论】:

标签: json angularjs date datetime angularjs-ng-repeat


【解决方案1】:

您可以将日期字符串传递给Date Object 把它放在你的控制器中:

myApp.controller("MyPersonController", function($scope){
    for (var i in $scope.persons) {
        $scope.persons[i].Date = new Date($scope.persons[i].Date);
    }
});

如果您在格式化时遇到问题,我会推荐一个日期包装器,例如 MomentJs

【讨论】:

    猜你喜欢
    • 2013-09-18
    • 2020-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-03
    • 2014-08-22
    • 2023-03-09
    • 2015-11-06
    相关资源
    最近更新 更多