第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何從現(xiàn)在開始的夜間時間數(shù)組中獲取接下來的幾個小時?

如何從現(xiàn)在開始的夜間時間數(shù)組中獲取接下來的幾個小時?

慕絲7291255 2023-03-24 14:07:02
我有一個這樣的小時數(shù)組['10:00:00', '11:00:00', '13:00:00', '14:00:00', '01:00:00'],我必須在其中過濾并獲取緊鄰現(xiàn)在的所有小時,所以如果現(xiàn)在是13:00:00我必須停止的小時'10:00:00' and '11:00:00',但午夜后的小時數(shù)>= 00:00:00應(yīng)該在該數(shù)組中。我試圖通過使用來做這樣的事情.filterconst 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;});但是由于明顯的原因(午夜后的日期是過去的,因為當天是同一天),午夜后的時間應(yīng)該在數(shù)組中的測試失敗了。這是我要實現(xiàn)的示例:如果我的時間數(shù)組如下:['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' ];現(xiàn)在的時間是16:00:00在過濾時間之后,我需要一個返回數(shù)組['16:30:00', '00:00:00', '01:00:00', '02:00:00', '02:30:00' ]
查看完整描述

4 回答

?
慕碼人8056858

TA貢獻1803條經(jīng)驗 獲得超6個贊

假設(shè)數(shù)組總是有序的,首先是今天的時間,然后是午夜后的時間:


const orari = [

    '10:00:00',

    '11:00:00',

    '12:00:00',

    '15:00:00',

    '16:00:00',

    '16:30:00',

    '00:00:00',

    '01:00:00',

    '02:00:00',

    '02:30:00',

]


const currentTime = new Date().toString().match(/\d{2}:\d{2}:\d{2}/)[0]


const cutoffIndex = orari.findIndex((hour, idx) =>

    hour.localeCompare(currentTime) > 0

    || (idx && hour.localeCompare(orari[idx - 1]) < 0))

// second condition required in case array contains

// _only_ times before now on same day + times after midnight


const filtered = orari.slice(cutoffIndex)


console.log(`Times after ${currentTime} -`, filtered)


查看完整回答
反對 回復(fù) 2023-03-24
?
吃雞游戲

TA貢獻1829條經(jīng)驗 獲得超7個贊

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 = null


hours.forEach((time, idx) => {

  let hour = parseInt(time.split(':')[0])

  let min = parseInt(time.split(':')[1])


  if (hour == curHour && min >= curMin || hour > curHour) {

    sliceIdx = sliceIdx === null ? idx : sliceIdx

  }

})


let newHours = hours.slice(sliceIdx + 1)


console.log(newHours)


查看完整回答
反對 回復(fù) 2023-03-24
?
撒科打諢

TA貢獻1934條經(jīng)驗 獲得超2個贊

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);

如果您需要檢查數(shù)組中是否存在時間,您可以將 indexOf 的值保存在另一個變量中(如果不存在則返回 -1);


查看完整回答
反對 回復(fù) 2023-03-24
?
墨色風(fēng)雨

TA貢獻1853條經(jīng)驗 獲得超6個贊

因為沒有辦法用我當前的時間數(shù)組來實現(xiàn)我正在尋找的東西,而且正如@lionel-rowe 提到的,我無法區(qū)分“今天早上 5 點”和“明天午夜后 5 點”,我不得不更改我的日期在我的數(shù)據(jù)庫中從時間輸入字符串,這樣我就可以通過這種方式在一個數(shù)組中添加第二天的所有夜間時間和同一天的早上時間:


如果時間是5AM我在 DB 中設(shè)置它,'05:00:00'在 JS 中我將它設(shè)置為它的日期,而用戶想在午夜后插入 5AM,我'29:00:00'在 JS 中設(shè)置小時我只是檢查小時是否是>= 24那么如果條件是true我將小時設(shè)置為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();


      const final = ore.map((o) => {

        const hour = o.split(':')[0];

        const min = o.split(':')[1];

        const date = new Date(giorno);

        if (Number(hour) >= 24) {

          date.setDate(date.getDate() + 1);

          date.setHours(Number(hour) - 24, Number(min), 0, 0);

        } else {

          date.setHours(Number(hour), Number(min), 0, 0);

        }

        return date;

      })

      .sort((a, b) => a.valueOf() - b.valueOf());


console.log(final.toLocaleString());


查看完整回答
反對 回復(fù) 2023-03-24
  • 4 回答
  • 0 關(guān)注
  • 152 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號