简体   繁体   中英

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 pm or 5:50 am

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 am for breakfast. 12:00 pm for lunch. 7:00 pm 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];

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 pm', '7:00 pm'].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 am")); console.log(timeToEat("5:50 pm")); console.log(timeToEat("6:30 pm"));
 .as-console-wrapper { max-height: 100%;important: top; 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, // Eg 2:30 pm 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 pm', 'breakfast': '7:00 am', 'dinner': '7:00 pm' }; 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)}` ));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM