Seraj Vahdati
Seraj Vahdati

Reputation: 512

javascript calculate the difference between two hours

There is a question that is tricky. Make a function that takes a string argument like 2:00 p.m. or 5:50 a.m.

You must not use momentjs or any other third-party library.

We have three static hours to determine the difference between them and this argument.

7:00 a.m. for breakfast. 12:00 p.m. for lunch. 7:00 p.m. for dinner.

The function should return an array with the first and second elements representing hours and minutes, like below:

eat("2:00 a.m.") // [5, 0];
eat("5:50 p.m.") // [1, 10];

Upvotes: 0

Views: 756

Answers (2)

RobG
RobG

Reputation: 147523

You need some basic functions to convert the time to some common unit that can be compared with some other time. In this case, minutes will do but seconds or milliseconds could also be used.

You also may need to allow for the case where the time is after the last meal, so the time will be the time to the first meal tomorrow.

The following is a simple implementation with some tests. Ideally the data would not be embedded in the timeToNextMeal function so that it's more easily maintained.

// Convert timestamp in h:mm ap format to minutes, 
// E.g. 2:30 p.m. is 870 minutes
function timeToMins(time) {
  // Get parts of time
  let [h, m, ap] = time.split(/\W/);
  // Set hours based on value and am/pm
  h = h%12 + (/^a/i.test(ap)? 0 : 12);
  // Return minutes
  return h*60 + m*1;
}

// Convert minutes to array [hours, minutes]
function minsToHM(mins) {
  return [mins/60 | 0, mins % 60];
}

function timeToNextMeal(date = new Date()) {
  let data = {
    'lunch'    : '12:00 p.m.',
    'breakfast': '7:00 a.m.',
    'dinner'   : '7:00 p.m.'
  };
  let minsToMeal = 0;
  // Get meal names, ensure sorted by time
  let meals = Object.keys(data).sort((a,b) =>
    timeToMins(data[a]) - timeToMins(data[b])
  );
  // Convert time to minutes
  let mins = date.getHours() * 60 + date.getMinutes();
  // Get next mealtime
  let nextMeal = meals.find(meal => timeToMins(data[meal]) > mins);
  // If undefined, next meal is first meal tomorrow
  if (!nextMeal) {
    minsToMeal += 1440 - mins;
    mins = 0;
    nextMeal = meals[0];
  }
  // Get minutes to next meal
  minsToMeal += timeToMins(data[nextMeal]) - mins;
  // Convert minutes to array [H,m]
  return minsToHM(minsToMeal);
}

// Examples
[new Date(2022,0,6, 4),    //  4 am
 new Date(2022,0,6, 5,58), //  5:58 am
 new Date(2022,0,6, 9,30), //  9:30 am
 new Date(2022,0,6,17,30), //  5:30 pm
 new Date(2022,0,6,20, 0), //  8:00 pm
].forEach(d => console.log(
 `${d.toLocaleTimeString('en-NZ')} ${timeToNextMeal(d)}`
));

Upvotes: 0

Terry Lennox
Terry Lennox

Reputation: 30725

You can start by creating a minutesSinceMidnight() function to get the time since midnight in minutes for a given input string.

We'll then create the timeToEat() function, which will start by finding the next meal time.

Once this is found, we'll get the time to the next meal in minutes, and convert to hours and minutes using a minutesToHoursAndMinutes() function.

function minutesSinceMidnight(timeStr) {
    let rg = /(\d{1,2})\:(\d{1,2})\s+([ap])\.?m/
    let [,hour, minute, am] = rg.exec(timeStr);
    hour = Number(hour);
    if (am === 'a' && hour === 12) hour -= 12;
    if (am === 'p' && hour < 12) hour += 12;
    return hour * 60 + Number(minute);
}

function minutesToHoursAndMinutes(totalMinutes) {
    let hours = Math.floor(totalMinutes / 60);
    let minutes = totalMinutes % 60;
    return [ hours, minutes]
}

function timeToEat(timeStr) {
    let currentTime = minutesSinceMidnight(timeStr);
    let mealTimes = ['7:00 a.m', '12:00 p.m.', '7:00 p.m.'].map(minutesSinceMidnight);
    let nextMealTime = mealTimes.find(mealTime => mealTime >= currentTime);
    // No meal found...
    if (nextMealTime === undefined) {
        return nextMealTime;
    }
    let timeToNextMealMinutes = nextMealTime - currentTime;
    return minutesToHoursAndMinutes(timeToNextMealMinutes);
} 

console.log(timeToEat("2:00 a.m."));
console.log(timeToEat("5:50 p.m."));
console.log(timeToEat("6:30 p.m."));
.as-console-wrapper { max-height: 100% !important; top: 0; }

Upvotes: 3

Related Questions