日期格式为json的角度

时间:2015-09-06 15:33:18

标签: javascript json angularjs date

    .controller('feedCtrl', ['$scope', '$http', function($scope, $http) {
  $http.get('items.json').then(function(response) {
    $scope.items = response.data;
    $scope.user = localStorage.getItem("glittrLoggedin");

  });

}])



JSON file:

[
  {
    "_id": "55e76d010ea262760117d43c",
    "title": "sit",
    "picture": "https://placeimg.com/640/480/any",
    "company": {
      "title": "COMVEYOR",
      "email": "hello@gmail.com",
      "phone": "+1 (845) 472-3160"
    },
    "about": "Tempor aute incididunt commodo ad aute do sint laboris. Officia adipisicing enim adipisicing aliquip laborum amet dolore ullamco. Id sunt enim consectetur aute adipisicing ullamco ad non tempor commodo proident. Irure pariatur quis fugiat ad mollit minim commodo pariatur do velit excepteur laboris ipsum culpa. Ea ea exercitation nostrud laborum laborum quis nisi. Aliqua ipsum enim occaecat velit mollit commodo pariatur. Magna ea laboris est et elit quis excepteur incididunt aute anim pariatur dolore irure cupidatat.",
    "createdAt": "Wed Apr 30 2014 18:57:48 GMT+0000 (UTC)"
  },
  {
    "_id": "55e76d017700a7889c317092",
    "title": "in",
    "picture": "https://placeimg.com/640/480/any",
    "company": {
      "title": "RODEMCO",
      "email": "hello@gmail.com",
      "phone": "+1 (821) 468-2057"
    },
    "about": "Ea in minim cupidatat id reprehenderit. Ut voluptate do mollit sit ullamco aliquip. Esse dolore labore velit ullamco nostrud tempor sit exercitation aute consectetur excepteur dolore occaecat. Tempor elit incididunt laborum esse dolor magna.",
    "createdAt": "Tue Apr 29 2014 20:52:30 GMT+0000 (UTC)"
  }
]

我正在尝试格式化' createdAT'格式为yyyy-MM-dd。我在我的HTML中尝试了以下内容,但仍然以相同的格式获取代码。

 {{item.createdAt | date :'yyyy-MM-dd'}}

代码链接:plunkrlink

1 个答案:

答案 0 :(得分:2)

您需要将日期字符串转换为date对象以对其应用date过滤器,因为您需要在控制器convertToDate内创建一个能够为您提供的功能日期对象,然后在其上应用过滤器。

<强>标记

<text fill="#ffffff" font-size="10" font-family="Verdana" x="20" y="66">
   {{convertToDate(item.createdAt)| date: 'yyyy-MM-dd'}}
</text>

<强>代码

$scope.convertToDate = function(date) {
    var dateOut = new Date(date);
    dateOut.setDate(new Date(date).getDate() + 1);
    return dateOut;
};

Working Demo