【问题标题】:Find closest next day from dates array, using Moment.js使用 Moment.js 从日期数组中查找最近的第二天
【发布时间】:2020-02-05 16:53:41
【问题描述】:

我有一个数组:

dates = [
"2019-10-04T12:38:02.506204+00:00",
"2019-10-04T14:59:39.370487+00:00",
"2019-10-04T14:59:50.897597+00:00",
"2019-10-04T14:59:57.344401+00:00",
"2019-10-04T15:00:04.631711+00:00",
"2019-10-04T15:00:13.460667+00:00",
"2019-10-04T15:00:21.672496+00:00",
"2019-10-04T15:00:29.643194+00:00",
"2019-10-04T15:00:37.022307+00:00",
"2019-10-04T15:01:00.134239+00:00",
"2019-10-04T15:01:08.146224+00:00",
"2019-10-04T15:01:15.205999+00:00",
"2019-10-04T15:01:21.877861+00:00",
"2019-10-04T15:01:28.089368+00:00",
"2019-10-04T16:29:05.438103+00:00",
"2019-10-04T16:29:49.927139+00:00",
"2019-10-05T16:35:00.994699+00:00",
"2019-10-06T08:45:47.289637+00:00",
"2019-10-06T08:46:11.294362+00:00",
"2019-10-06T08:46:23.702585+00:00",
"2019-10-06T08:46:33.374726+00:00",
"2019-10-06T08:46:42.726666+00:00",
"2019-10-06T08:46:52.916887+00:00",
"2019-10-06T08:47:02.659273+00:00",
"2019-10-06T16:08:14.975139+00:00",
"2019-10-06T16:08:29.047735+00:00",
"2019-10-06T16:08:42.831487+00:00",
"2019-10-06T16:08:58.472631+00:00",
"2019-10-07T08:32:43.248706+00:00",
"2019-10-07T08:37:00.512249+00:00",
"2019-10-07T08:38:12.188744+00:00",
"2019-10-07T08:40:08.362515+00:00",
"2019-10-07T08:41:57.151322+00:00",
"2019-10-07T08:46:52.107963+00:00",
"2019-10-07T08:53:38.604697+00:00",
"2019-10-07T08:53:53.449463+00:00",
"2019-10-07T09:15:31.659223+00:00"
]

我需要用两个按钮进行切换,第一个按钮将日期向前切换,第二个按钮向后切换。 日期从数组中使用。

请帮助 JS。

【问题讨论】:

  • 您如何确定“第二天”的含义?相对于今天?相对于另一个日期?
  • @VLAZ 我有 "currentDate" var - 数组中的最小日期。
  • 那么您想要下一个日期 ("2019-10-04T14:59:39.370487+00:00") 还是新一天的下一个日期 ("2019-10-05T16:35:00.994699+00:00")?

标签: javascript arrays date momentjs


【解决方案1】:

您可以在 Moment.js 对象上使用 isBefore 方法。如果您将第二个参数作为"day" 传递,那么它会认为:

  • "2019-10-04T12:38:02.506204+00:00"(10 月 4 日)早于 "2019-10-05T16:35:00.994699+00:00"(10 月 5 日)
  • "2019-10-04T12:38:02.506204+00:00"(10 月 4 日)早于 "2019-10-04T14:59:39.370487+00:00"(10 月 4 日)

let dates = [
  "2019-10-04T12:38:02.506204+00:00",
  "2019-10-04T14:59:39.370487+00:00",
  "2019-10-04T14:59:50.897597+00:00",
  "2019-10-04T14:59:57.344401+00:00",
  "2019-10-04T15:00:04.631711+00:00",
  "2019-10-04T15:00:13.460667+00:00",
  "2019-10-04T15:00:21.672496+00:00",
  "2019-10-04T15:00:29.643194+00:00",
  "2019-10-04T15:00:37.022307+00:00",
  "2019-10-04T15:01:00.134239+00:00",
  "2019-10-04T15:01:08.146224+00:00",
  "2019-10-04T15:01:15.205999+00:00",
  "2019-10-04T15:01:21.877861+00:00",
  "2019-10-04T15:01:28.089368+00:00",
  "2019-10-04T16:29:05.438103+00:00",
  "2019-10-04T16:29:49.927139+00:00",
  "2019-10-05T16:35:00.994699+00:00",
  "2019-10-06T08:45:47.289637+00:00",
  "2019-10-06T08:46:11.294362+00:00",
  "2019-10-06T08:46:23.702585+00:00",
  "2019-10-06T08:46:33.374726+00:00",
  "2019-10-06T08:46:42.726666+00:00",
  "2019-10-06T08:46:52.916887+00:00",
  "2019-10-06T08:47:02.659273+00:00",
  "2019-10-06T16:08:14.975139+00:00",
  "2019-10-06T16:08:29.047735+00:00",
  "2019-10-06T16:08:42.831487+00:00",
  "2019-10-06T16:08:58.472631+00:00",
  "2019-10-07T08:32:43.248706+00:00",
  "2019-10-07T08:37:00.512249+00:00",
  "2019-10-07T08:38:12.188744+00:00",
  "2019-10-07T08:40:08.362515+00:00",
  "2019-10-07T08:41:57.151322+00:00",
  "2019-10-07T08:46:52.107963+00:00",
  "2019-10-07T08:53:38.604697+00:00",
  "2019-10-07T08:53:53.449463+00:00",
  "2019-10-07T09:15:31.659223+00:00"
]

let currentDate = moment(dates[0]); //pick a spot for the "current time"

//sort the dates to ensure they are sequential
dates.sort();

let nextDate = dates.
  filter(isoDate => currentDate.isBefore(isoDate, "day")) //filter anything before the current
   [0]; //take the first (lowest) date

console.log(nextDate);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>

【讨论】:

    猜你喜欢
    • 2019-01-19
    • 1970-01-01
    • 1970-01-01
    • 2019-02-26
    • 1970-01-01
    • 2012-08-01
    • 2019-07-09
    • 1970-01-01
    • 2021-09-27
    相关资源
    最近更新 更多