Hatim Fayez
Hatim Fayez

Reputation: 231

Compare between two times and get the next using JavaScript

I have a different set of time, "4:30 AM", "12:30 PM", "3:30 PM" , "7:00 PM", and "9:15 PM" I did put all in an array and already parsed. Also I am comparing between them but I don't know which logic to use to get the next time from current time not the nearest

For example if it's now "1:00 PM" the next suppose to be "3:30 PM" while the nearest is "12:30 PM"

The code I am using :

let times= new Array("4:30 AM", "12:30 PM", "3:30 PM" , "7:00 PM", "9:15 PM");
let currentTime = new Date();
let currentHour = parseInt(currentTime.getHours());
let availableDates = times;
let convertedHours = availableDates.map((date) => {
    let time = parseInt(date.split(' ')[0]);

    let period = date.split(' ')[1];

    if(time === 12 && period === 'pm' )
      return time;

    if(time < 12 && period === 'am')
      return time; 

    return time + 12;
});

After that I don't know which logic will gonna achieve what I want

Upvotes: 0

Views: 495

Answers (2)

FujiRoyale
FujiRoyale

Reputation: 832

If I understand your post correctly you are looking find the next nearest fit for an array of strings that resemble times. Assuming days are not important then this will get the job done using native js dateTimes and the array.find.

Basically I assume we just need to slot whatever time provided into the next nearest time provided in the array. This first converts the times to dateTime Objects then compares them against the provided time from the dateTime set at currentTime.

let times= new Array("4:30 AM", "12:30 PM", "3:30 PM" , "7:00 PM", "9:15 PM");
const currentTime = new Date(new Date().toDateString() + ' ' + '19:30');
const availableDates = times;

const nextSlot = availableDates.find((date) => {
    let timeH = parseInt(date.split(' ')[0]);
    const timeM = parseInt(date.split(':')[1].split(' ')[0]);
    const period = date.split(' ')[1];
    if(period === "PM" && timeH !== 12) {
      timeH += 12;
    }
    const time = `${timeH}:${timeM}`

    const parseDate = new Date(new Date().toDateString() + ' ' + time);
    return currentTime < parseDate;

});

console.log("nextSlot", nextSlot);

Hope this helps and is what you are looking for.

Upvotes: 1

CevaComic
CevaComic

Reputation: 2114

If i understand correctly , I hope this helps :

let times = new Array("4:30 AM", "12:30 PM", "3:30 PM", "7:00 PM", "9:15 PM");
let currentTime = new Date();
let currentHour = parseInt(currentTime.getHours());
let availableDates = times;

function convertHour(time) {
    let parts = time.split(' ');
    time = parseInt(parts[0]);
    let period = parts[1];

    if (time === 12 && period === "PM") 
        return time;

    if (time < 12 && period === "AM") 
        return time;

    return time + 12;
}

function getNextTime(currentTime) {
    let index = -1;
    // find index of next
    for (let i = 0; i < availableDates.length; i++) {
        let converted = convertHour(availableDates[i]);
        if (converted > currentTime) {
            index = i;
            break;
        }
    }
    // return the value
    return index !== -1 ? availableDates[index] : undefined;
}

Upvotes: 1

Related Questions