为了账号安全,请及时绑定邮箱和手机立即绑定

使用 Moment.js 从日期数组中查找最接近的第二天

使用 Moment.js 从日期数组中查找最接近的第二天

红颜莎娜 2021-11-12 17:36:13
我有一个数组: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。
查看完整描述

1 回答

?
慕妹3146593

TA贡献1820条经验 获得超9个赞

您可以isBefore在 Moment.js 对象上使用该方法。如果您将第二个参数作为 传递"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"(月4日)是不是之前"2019-10-04T14:59:39.370487+00:00"(月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>



查看完整回答
反对 回复 2021-11-12
  • 1 回答
  • 0 关注
  • 114 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信