【问题标题】:AngularJS - Filter by dateAngularJS - 按日期过滤
【发布时间】:2019-01-14 20:39:05
【问题描述】:

我有一份来自 API结果项目列表。 在我看来,我用 ng-repeat 呈现了这个结果

<div ng-repeat="item in items"></div>

这是我的 API 回复。

2017-01-17T09:35:38.429Z
2017-01-03T10:00:00.000Z
2017-01-27T14:34:55.179Z

我需要按日期过滤,即如果项目超过 3 个月的显示。

【问题讨论】:

  • if the items are more than 3 months old show - 这是过滤,不是排序...
  • 感谢罗伊的帮助!!
  • 你能帮我解决这个问题吗?
  • 这里有很好的例子:https://www.w3schools.com/angular/ng_filter_orderby.asp 和这里https://www.w3schools.com/angular/angular_filters.asp

标签: angularjs date


【解决方案1】:

您根据您的情况展示该项目,在您的情况下不应超过 3 个月。

<div ng-repeat="item in items"> <div ng-if="!isOlderThan3Months(item)">item</div> </div>

function isOlderThan3Months(date){
   return new Date().getTime() - new Date(date).getTime() > 1000 /*ms*/ * 60/*s*/ * 60/*min*/ * 24/*h*/ * 30/*days*/ * 3/*months*/ )
}

其中condition 可以调用函数来计算日期之间的差异。查看here 了解执行此操作的多种方法。

【讨论】:

    【解决方案2】:

    假设它是一个数组:

    let items = ["2017-01-17T09:35:38.429Z",
    "2017-01-03T10:00:00.000Z",
    "2017-01-27T14:34:55.179Z"]
    

    这样做

    let threeMonthsAgo = new Date(Date.now() - 7776000000)
    items = items.filter(date => { threeMonthsAgo.before( (new Date(date)) ) })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-22
      • 2014-07-22
      • 2014-11-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多