如何从现在开始的夜间时间数组中获取接下来的几个小时?

我有一个这样的小时数组['10:00:00', '11:00:00', '13:00:00', '14:00:00', '01:00:00'],我必须在其中过滤并获取紧邻现在的所有小时,所以如果现在是13:00:00我必须停止的小时'10:00:00' and '11:00:00',但午夜后的小时数>= 00:00:00应该在该数组中。


我试图通过使用来做这样的事情.filter


const now = new Date();

const orari = [

 '10:00:00',

 '11:00:00',

 '12:00:00',

 '16:00:00',

 '16:30:00',

 '00:00:00',

 '01:00:00',

 '02:00:00',

 '02:30:00',

];

orari = orari.filter((o) => {

 return new Date(o) > now;

});

或者


const hours = new Date().getHours();

const orari = [

 '10:00:00',

 '11:00:00',

 '12:00:00',

 '16:00:00',

 '16:30:00',

 '00:00:00',

 '01:00:00',

 '02:00:00',

 '02:30:00',

];

orari = orari.filter((o) => {

 return Number(o.split(':')[0]) > hours;

});

但是由于明显的原因(午夜后的日期是过去的,因为当天是同一天),午夜后的时间应该在数组中的测试失败了。


这是我要实现的示例:


如果我的时间数组如下:['10:00:00', '11:00:00', '12:00:00', '16:00:00', 16:30:00', '00:00:00', '01:00:00', '02:00:00', '02:30:00' ];


现在的时间是16:00:00在过滤时间之后,我需要一个返回数组['16:30:00', '00:00:00', '01:00:00', '02:00:00', '02:30:00' ]


慕丝7291255
浏览 115回答 4
4回答

慕码人8056858

假设数组总是有序的,首先是今天的时间,然后是午夜后的时间:const orari = [&nbsp; &nbsp; '10:00:00',&nbsp; &nbsp; '11:00:00',&nbsp; &nbsp; '12:00:00',&nbsp; &nbsp; '15:00:00',&nbsp; &nbsp; '16:00:00',&nbsp; &nbsp; '16:30:00',&nbsp; &nbsp; '00:00:00',&nbsp; &nbsp; '01:00:00',&nbsp; &nbsp; '02:00:00',&nbsp; &nbsp; '02:30:00',]const currentTime = new Date().toString().match(/\d{2}:\d{2}:\d{2}/)[0]const cutoffIndex = orari.findIndex((hour, idx) =>&nbsp; &nbsp; hour.localeCompare(currentTime) > 0&nbsp; &nbsp; || (idx && hour.localeCompare(orari[idx - 1]) < 0))// second condition required in case array contains// _only_ times before now on same day + times after midnightconst filtered = orari.slice(cutoffIndex)console.log(`Times after ${currentTime} -`, filtered)

吃鸡游戏

let curDate = new Date()let curHour = 16//curDate.getHours()let curMin = 30//curDate.getMinutes()const hours=["10:00:00","11:00:00","12:00:00","16:00:00","16:30:00","00:00:00","01:00:00","02:00:00","02:30:00"];let sliceIdx = nullhours.forEach((time, idx) => {&nbsp; let hour = parseInt(time.split(':')[0])&nbsp; let min = parseInt(time.split(':')[1])&nbsp; if (hour == curHour && min >= curMin || hour > curHour) {&nbsp; &nbsp; sliceIdx = sliceIdx === null ? idx : sliceIdx&nbsp; }})let newHours = hours.slice(sliceIdx + 1)console.log(newHours)

撒科打诨

let array = ['10:00:00', '11:00:00', '12:00:00', '16:00:00', '16:30:00', '00:00:00', '01:00:00', '02:00:00', '02:30:00' ];let newArray = a.slice(a.indexOf('16:00:00') + 1, a.length);如果您需要检查数组中是否存在时间,您可以将 indexOf 的值保存在另一个变量中(如果不存在则返回 -1);

墨色风雨

因为没有办法用我当前的时间数组来实现我正在寻找的东西,而且正如@lionel-rowe 提到的,我无法区分“今天早上 5 点”和“明天午夜后 5 点”,我不得不更改我的日期在我的数据库中从时间输入字符串,这样我就可以通过这种方式在一个数组中添加第二天的所有夜间时间和同一天的早上时间:如果时间是5AM我在 DB 中设置它,'05:00:00'在 JS 中我将它设置为它的日期,而用户想在午夜后插入 5AM,我'29:00:00'在 JS 中设置小时我只是检查小时是否是>= 24那么如果条件是true我将小时设置为day + 1.所以代码看起来类似于:const ore = ["01:00:00", "08:30:00", "12:00:00", "12:30:00", "13:00:00", "13:30:00", "14:00:00", "14:30:00", "24:00:00", "25:00:00", "26:00:00"];const giorno = new Date();&nbsp; &nbsp; &nbsp; const final = ore.map((o) => {&nbsp; &nbsp; &nbsp; &nbsp; const hour = o.split(':')[0];&nbsp; &nbsp; &nbsp; &nbsp; const min = o.split(':')[1];&nbsp; &nbsp; &nbsp; &nbsp; const date = new Date(giorno);&nbsp; &nbsp; &nbsp; &nbsp; if (Number(hour) >= 24) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; date.setDate(date.getDate() + 1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; date.setHours(Number(hour) - 24, Number(min), 0, 0);&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; date.setHours(Number(hour), Number(min), 0, 0);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return date;&nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; .sort((a, b) => a.valueOf() - b.valueOf());console.log(final.toLocaleString());
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript