简体   繁体   中英

How convert hours and minutes into minutes using javascript?

在此处输入图像描述 I need to convert hours and minutes(30mins, 65mins) into minutes in a dropdown using javascript, If it's only 30 minutes, display as 30mins, if then 65mins, display 1hr 05mins in the text of a dropdown and value as 65 in value property, I tried some code, On the right side of the picture, it gives the value 1hr60mins not 2hrs! In the code at last line, I just console the object,

 var tObj = [ {"hr": 0, "min": 30, "inMin": 0, "value": "" }, {"hr": 0, "min": 45, "inMin": 0, "value": "" }, {"hr": 1, "min": 60, "inMin": 0, "value": "" } ]; tObj.forEach(function(obj){ if(obj.min > 0){ if(obj.hr > 0){ obj.inMin = (obj.hr * 60 + obj.min); obj.value = obj.hr + "hr" + obj.min + "mins"; }else{ if(obj.min === 60){ obj.inMin = Math.floor(obj.min / 60); obj.value = obj.inMin + "hr"; }else{ obj.inMin = obj.min; obj.value = obj.min + "mins"; } } }else{ if(obj.hr > 0){ obj.inMin = (obj.hr * 60); obj.value = obj.hr + "hr"; }else{ obj.inMin = 0; obj.value = obj.min + "mins"; } } }); console.log(tObj);

If I understood correctly what you want to achieve, this should do the job:

 // SOLUTION TO THE EDITED QUESTION const tObj2 = Object.freeze([{ "hr": 0, "min": 30, "inMin": 0, "value": "" }, { "hr": 0, "min": 45, "inMin": 0, "value": "" }, { "hr": 0, "min": 50, "inMin": 0, "value": "" }, { "hr": 1, "min": 15, "inMin": 0, "value": "" }, { "hr": 1, "min": 30, "inMin": 0, "value": "" }, { "hr": 1, "min": 45, "inMin": 0, "value": "" }, { "hr": 2, "min": 0, "inMin": 0, "value": "" }, { "hr": 1, "min": 65, "inMin": 0, "value": "" } ]); tObj2.forEach(item => { const hours = item.hr + Math.floor(item.min / 60); const hoursTxt = hours > 0? hours + (hours === 1 && 'hr' || 'hrs'): ''; const minutes = item.min % 60; const minutesTxt = minutes + (minutes === 1 && 'min' || 'mins'); const timeTxt = hoursTxt? hoursTxt + ' ' + minutesTxt: minutesTxt; item.inMin = hours * 60 + minutes; item.value = timeTxt; }); // SOLUTION TO THE ORIGINAL QUESTION const tObj = Object.freeze([{ "hr": 0, "min": 30 }, { "hr": 0, "min": 60 }, { "hr": 1, "min": 0 }, { "hr": 1, "min": 45 }, { "hr": 2, "min": 45 }, { "hr": 0, "min": 60 } ]); const formattedTimes = tObj.reduce((acc, item) => { const hours = item.hr + Math.floor(item.min / 60); const hoursTxt = hours > 0? hours + (hours === 1 && 'hr' || 'hrs'): ''; const minutes = item.min % 60; const minutesTxt = minutes + (minutes === 1 && 'min' || 'mins'); const timeTxt = hoursTxt? hoursTxt + ' ' + minutesTxt: minutesTxt; const timeObj = { value: hours * 60 + minutes, text: timeTxt } acc.push(timeObj); return acc }, []); //test console.log('Solution to the edited question:', tObj2); console.log('Solution to the original question:', formattedTimes);

I've been a bit verbose with the code, in the hope it is self-explanatory but it can be shortened a bit, if you want.

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