There are a couple parts to this question. I am not opposed to using a jQuery plugin if anyone knows of one that will accomplish what I want done.
Q1 – How do I convert minutes into hours and vise versa? For example how would I convert 90 minutes into 1 hour and 30 minutes. In the code below how would I get “totalMin” converted and displayed in “convertedHour” and “convertedMin”?
HTML:
<span class="totalMin">90</span> Minutes
<span class="convertedHour">0</span> Hours
<span class="convertedMin">0</span> Minutes
jsFiddle: http://jsfiddle.net/ExFBD
Q2 – How would I add up a group of time slots? For example, How would I add 1 hour 30 mins, 2 hours 45 mins and 2 hour 15 mins together?
HTML:
<span class="hour-1 hour">1</span> hour
<span class="min-1 min">30</span> min
<span class="hour-2 hour">2</span> hour
<span class="min-2 min">45</span> min
<span class="hour-3 hour">2</span> hour
<span class="min-3 min">15</span> min
<span class="totalHour">0</span> hour
<span class="totalMin">0</span> min
jsFiddle: http://jsfiddle.net/DX9YA/
Q3 – How would I take a time value (3 hours and 30 mins) and add it to a real time stamp such as 10:30 AM? How would I take AM vs PM into account?
Q1:
Q2:
The function below will take as input # of minutes and output time in the following format: Hours:minutes.
I used Math.trunc(), which is a new method added in 2015. It returns the integral part of a number by removing any fractional digits.
In case you want to return a string in 00:00 format…
Thanks for the up-votes. Here’s a slightly tidier ES6 version 🙂
this will log 1:5 to the console. It is a short and simple solution that should be easy to understand and no jquery plugin is necessary…
Alternated to support older browsers.
As the above answer of ConnorLuddy can be slightly improved, there are a minor change to formula to convert minutes to
hours:mins
formatMy theory is that we can not predict that `mins` value will always be an integer.
The added `Math.round` to function will correct the output.
For example: when the minutes=125.3245, the output will be 02:05 with this fix, and 02:05.3245000000000005 without the fix.
Hope that someone need this!
This code can be used with timezone
javascript:
I took the liberty of modifying ConorLuddy’s answer to address both 24 hour time and 12 hour time.
👍 Very short code for transferring minutes in two formats!