Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

420
Views
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];
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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

about 4 years ago · Juan Pablo Isaza Report

0

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

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!