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

我有一个数组:


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。


红颜莎娜
浏览 118回答 1
1回答

慕妹3146593

您可以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 = [&nbsp; "2019-10-04T12:38:02.506204+00:00",&nbsp; "2019-10-04T14:59:39.370487+00:00",&nbsp; "2019-10-04T14:59:50.897597+00:00",&nbsp; "2019-10-04T14:59:57.344401+00:00",&nbsp; "2019-10-04T15:00:04.631711+00:00",&nbsp; "2019-10-04T15:00:13.460667+00:00",&nbsp; "2019-10-04T15:00:21.672496+00:00",&nbsp; "2019-10-04T15:00:29.643194+00:00",&nbsp; "2019-10-04T15:00:37.022307+00:00",&nbsp; "2019-10-04T15:01:00.134239+00:00",&nbsp; "2019-10-04T15:01:08.146224+00:00",&nbsp; "2019-10-04T15:01:15.205999+00:00",&nbsp; "2019-10-04T15:01:21.877861+00:00",&nbsp; "2019-10-04T15:01:28.089368+00:00",&nbsp; "2019-10-04T16:29:05.438103+00:00",&nbsp; "2019-10-04T16:29:49.927139+00:00",&nbsp; "2019-10-05T16:35:00.994699+00:00",&nbsp; "2019-10-06T08:45:47.289637+00:00",&nbsp; "2019-10-06T08:46:11.294362+00:00",&nbsp; "2019-10-06T08:46:23.702585+00:00",&nbsp; "2019-10-06T08:46:33.374726+00:00",&nbsp; "2019-10-06T08:46:42.726666+00:00",&nbsp; "2019-10-06T08:46:52.916887+00:00",&nbsp; "2019-10-06T08:47:02.659273+00:00",&nbsp; "2019-10-06T16:08:14.975139+00:00",&nbsp; "2019-10-06T16:08:29.047735+00:00",&nbsp; "2019-10-06T16:08:42.831487+00:00",&nbsp; "2019-10-06T16:08:58.472631+00:00",&nbsp; "2019-10-07T08:32:43.248706+00:00",&nbsp; "2019-10-07T08:37:00.512249+00:00",&nbsp; "2019-10-07T08:38:12.188744+00:00",&nbsp; "2019-10-07T08:40:08.362515+00:00",&nbsp; "2019-10-07T08:41:57.151322+00:00",&nbsp; "2019-10-07T08:46:52.107963+00:00",&nbsp; "2019-10-07T08:53:38.604697+00:00",&nbsp; "2019-10-07T08:53:53.449463+00:00",&nbsp; "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 sequentialdates.sort();let nextDate = dates.&nbsp; filter(isoDate => currentDate.isBefore(isoDate, "day")) //filter anything before the current&nbsp; &nbsp;[0]; //take the first (lowest) dateconsole.log(nextDate);<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript