NiceToMytyuk
NiceToMytyuk

Reputation: 4277

How to get the next hours from now from an Array with night hours?

I have an array of hours like this ['10:00:00', '11:00:00', '13:00:00', '14:00:00', '01:00:00'] where i have to filter and get all hours that are next to now, so if now the hour is 13:00:00 i have to cut off '10:00:00' and '11:00:00' but hours after midnight >= 00:00:00 should be in that array.

I was trying to do something like this by using .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;
});

Or

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

But for obvious reasons (date after midnight is in the past as the day is the same) the test where times after midnight should be in the array fails.

Here is an example of what i would archieve:

If my time array is the following: ['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' ];

And Time now is 16:00:00 after filtering the times i need a return array of ['16:30:00', '00:00:00', '01:00:00', '02:00:00', '02:30:00' ]

Upvotes: 0

Views: 335

Answers (4)

NiceToMytyuk
NiceToMytyuk

Reputation: 4277

As there were no way to archieve what i was looking for with my current time array, and as mentioned by @lionel-rowe that i can't distinguish "5AM earlier today" from "5AM after midnight tomorrow" i had to change my date type in my DB from Time to string, in this way i'm able to add all night hours of the next day and morning hours of same day in one array in this way:

If the time is 5AM i'm setting it in DB as '05:00:00' and in JS i'm setting it with it's date, while the user want to insert 5AM after midnight i'm setting the hour as '29:00:00' then in JS i just check if the hour is >= 24 then if the condition is true i'm setting the hour to it's day + 1.

So the code looks like similar to this:

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

Upvotes: 0

symlink
symlink

Reputation: 12209

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)

Upvotes: 1

BARNI
BARNI

Reputation: 142

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

if you need to check if time exists in your array you can save value of indexOf in another variable (if it does not exists it returns -1);

Upvotes: 0

Lionel Rowe
Lionel Rowe

Reputation: 5926

Assuming the array is always ordered, with times today first, followed by times after midnight:

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)

Upvotes: 1

Related Questions