【问题标题】:Get only futures dates in array仅获取数组中的期货日期
【发布时间】:2018-04-06 23:16:03
【问题描述】:

我在数组中查找未来日期的总数,有些键没有值。

我已经设法将新数组中的日期分开以仅获取未来的日期:

let dates = resp.consultas.map(consultas => consultas.date_a);

结果:

[
  "2018-04-02T15:15",
  "2018-04-07T14:20",
  "2018-04-28T11:50",
  null,
  null,
  null,
  null,
  "2018-05-30T17:45",
  "2018-04-07T16:20"
]

我如何获得未来日期的计数?

【问题讨论】:

  • 你在这里遗漏了一些东西。是否需要引用原始对象?
  • 我还注意到这些是没有时区的时间戳。您是否需要它们作为本地或它们存储为 UTC 并且在返回时未正​​确表示?

标签: javascript arrays date


【解决方案1】:
// Get current time
const now = Date.now();
const futureDates = dates.filter(date => {
  // Filter out dates in the past or falsey values
  return date && (new Date(date)).getTime() > now;
});

// Do something with the result
console.log(futureDates.length);

【讨论】:

    【解决方案2】:

    您可以使用moment.isAfterArray.filter 来清除旧日期和null 值。

    const dates = [
      "2018-04-02T15:15",
      "2018-04-07T14:20",
      "2018-04-28T11:50",
      null,
      null,
      null,
      null,
      "2018-05-30T17:45",
      "2018-04-07T16:20"
    ]
    
    const futureDates = dates.filter(date => moment(date).isAfter())
    
    console.log(futureDates);
    <script src="https://momentjs.com/downloads/moment.js"></script>

    我如何获得未来日期的计数?

    使用过滤器后,只剩下有效日期,所以:

    console.log(futureDates.length);
    

    【讨论】:

    • 当然,如果您要花时间做很多工作,那么 Moment 是不错的选择。也就是说,为此Date API 就足够了。
    • 是的,我知道,但是由于使用Date 至少还有两个其他答案,我想提供一个替代方案。现在它是最好的 JS 日期库 imho,所以 OP 可能会学到另一件事:)
    • 别说我在用 Angular,这似乎是个小问题。我的应用程序中已经内置了角矩。因此,添加回库并不是最好的。谢谢你的回答!
    • 你可以访问moment,如果你已经在使用angular-moment,你不需要添加库。 If you wish to use moment() in your services, controllers, or directives, simply inject the moment variable into the constructor.
    【解决方案3】:

    如果不考虑时区、无效日期字符串等情况,请使用Array.filter,如下所示:

    const test = [
      "2018-04-02T15:15",
      "2018-04-07T14:20",
      "2018-04-28T11:50",
      null,
      null,
      null,
      null,
      "2018-05-30T17:45",
      "2018-04-07T16:20"
    ]
    const currentWhenCalc = new Date()
    result = test.filter(function(item){
      return item && new Date(item) > currentWhenCalc
    })
    console.log('data', result, 'count', result.length)

    【讨论】:

    • 我会小心这段代码,因为没有检查空值并且today(又名“现在)的时间是预先计算的。当将null传递给new Date时将为您提供当前时间。这意味着在计算 today 值与将 null 值与其进行比较之间存在轻微延迟时,最终结果中可能会出现空值。
    • 感谢您更新代码以响应我的评论 :)
    • 当您将 null 传递给 Date 时,您会在 Chrome/Node/FF 中获得 epoch 0
    【解决方案4】:

    实际上你甚至不需要去嵌套它们。如果你想过滤一个数组,你应该使用Array.filter。 IT有(value, index, originalArray) => Boolean的足迹,真意为keep,假意为remove:

    let currentTime = new Date();
    let futureconsultas = resp.consultas.filter( consultas =>
      new Date(consultas.date_a) - currentTime > 0
    );
    

    现在您应该有一个包含所有未来日期的数组
    - 注意:new Date() - new Date('INVALID') 返回NaN,当与数字比较或被屏蔽时,其评估结果为 false,因此您不必担心日期错误。 new Date(null) 参数导致 epoch 0,这不是现在之前,new Date(undefined) 导致错误日期
    - 第二个注意事项:您不应该在每个循环中使用new Date() 调用。如果数组足够大,这会减慢执行速度。获取引用日期并使用闭包(范围)来引用它。

    这是有效的,因为当您计算两个 Dates 时,它们的结果实际上是基于日期的纪元值的数字。

    一个简化的例子:

    const filtration = object => object.v > 3
    console.log([{v:1},{v:2},{v:3},{v:4},{v:5}].filter(filtration)) // [{v:4},{v:5}]
    

    之后,您只需检查数组上的.length 属性

    [1,2,3,4,5].length == 5 // true
    

    Array 在 JS 中的原型上有几个有用的函数,包括 forEach(mutating)、filter、map、find、findIndex 和 more

    【讨论】:

    • 您的代码工作正常,但打字稿在部署应用程序 (Angular) 时标记了一个错误。 The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type. 因此,标记为正确的另一个答案也有效。谢谢!
    • 奇数。由于强制,它应该始终是用于比较的数字
    【解决方案5】:

    日期是示例日期中的数组(作为字符串)。 Array 有一个 filter 方法,它为你的 dates 数组中的每个日期执行一个提供的函数。然后检查该日期是否实际上有一个值(非空),创建新的日期来表示今天的日期(dt)和来自数组中的字符串的日期。 Date.getTime() 是一种比较“时间刻度”的方法,是比较两个日期对象的好方法。如果您的日期(将来)大于今天,则该日期将添加到新数组中,该数组根据此条件构建过滤函数(futureDates),最终将仅包含将来的元素。

    var futureDates = dates.filter(function(date) {
       if(date) {
         var dt = new Date();
         var df = new Date(date);
         if(df.getTime() > dt.getTime())
           return df;
         }
    });
    

    【讨论】:

    • 只需return df.getTime() > dt.getTime(),不需要用if else包装
    • 是的,这很简短,我是为了清晰而不是简洁的答案。
    • 您能否提供解释以及您的代码?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-23
    • 2020-07-16
    相关资源
    最近更新 更多