3

My question is: How to convert minutes to time(hh:mm:ss)?

I have a value with:

decimalMinuteString="1.1783471074380165"

Expected output is: "00:01:10"

How to do with javascript/jquery?

Thanks

ManishDalwadi
  • 41
  • 1
  • 1
  • 5
  • Have you looked at momnetjs? - http://momentjs.com/ – soundslikeodd Dec 30 '16 at 06:05
  • 1
    reference: https://gist.github.com/tamboer/7316306 – dhruv jadia Dec 30 '16 at 06:08
  • This question has been answered many times before (as I have also wondered how to do this). Did you check SO before asking? Just wondering – zerohero Dec 30 '16 at 06:46
  • 1
    Possible duplicate of [Get the time difference between two datetimes](https://stackoverflow.com/questions/18623783/get-the-time-difference-between-two-datetimes) – Akhil Mathew Oct 05 '17 at 09:25
  • Does this answer your question? [How to convert minutes to hours/minutes and add various time values together using jQuery?](https://stackoverflow.com/questions/4687723/how-to-convert-minutes-to-hours-minutes-and-add-various-time-values-together-usi) – Michael Freidgeim Feb 06 '20 at 07:42

3 Answers3

13

I needed to convert X minutes to HH:MM (Hours:Minutes), I used the following code to accomplish this:

MINUTES = X; //some integer

var m = MINUTES % 60;

var h = (MINUTES-m)/60;

var HHMM = (h < 10 ? "0" : "") + h.toString() + ":" + (m < 10 ? "0" : "") + m.toString();
blondelg
  • 916
  • 1
  • 8
  • 25
Z.T
  • 485
  • 1
  • 5
  • 8
4

A very neat answer has been given by powtac in another question, where seconds were needed to be converted to the same format.

Changing his provided solution to fit your issue, the following prototype function can be used to convert minutes to HH:MM:SS string format.

String.prototype.minsToHHMMSS = function () {
    var mins_num = parseFloat(this, 10); // don't forget the second param
    var hours   = Math.floor(mins_num / 60);
    var minutes = Math.floor((mins_num - ((hours * 3600)) / 60));
    var seconds = Math.floor((mins_num * 60) - (hours * 3600) - (minutes * 60));

    // Appends 0 when unit is less than 10
    if (hours   < 10) {hours   = "0"+hours;}
    if (minutes < 10) {minutes = "0"+minutes;}
    if (seconds < 10) {seconds = "0"+seconds;}
    return hours+':'+minutes+':'+seconds;
}

// Use it as following:
myDecimalNumber.minsToHHMMSS();

See the working code in the snippet below:

String.prototype.minsToHHMMSS = function () {
    var mins_num = parseFloat(this, 10); // don't forget the second param
    var hours   = Math.floor(mins_num / 60);
    var minutes = Math.floor((mins_num - ((hours * 3600)) / 60));
    var seconds = Math.floor((mins_num * 60) - (hours * 3600) - (minutes * 60));

    // Appends 0 when unit is less than 10
    if (hours   < 10) {hours   = "0"+hours;}
    if (minutes < 10) {minutes = "0"+minutes;}
    if (seconds < 10) {seconds = "0"+seconds;}
    return hours+':'+minutes+':'+seconds;
}

var decimalMinuteString = '1.1783471074380165';

var timeString = decimalMinuteString.minsToHHMMSS();

var input = document.getElementById('input');
var output = document.getElementById('output');
input.innerText = decimalMinuteString;
output.innerText = timeString;
<p>
  Input: <span id="input"></span> 
</p>
<p>
  Output: <span id="output"></span>  
</p>

If this solution helped you, please upvote firstly powtac's answer as it is the base of the answer.

Community
  • 1
  • 1
Siavas
  • 4,992
  • 2
  • 23
  • 33
  • Yet, it did not worked for large values. For example: for **63240.2208** it shows **1054:00:13** –  Jul 09 '19 at 05:37
  • @user8795381 it works as expected. If you want to days and weeks you should extend the snippet or write one for that purpose. – Chris Pillen Sep 26 '21 at 06:22
3

One line is:

new Date(minutes * 60 * 1000).toISOString().substr(11, 8);
Pavel
  • 31
  • 1