最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to convert minutes to time(hh:mm:ss)? - Stack Overflow

programmeradmin3浏览0评论

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

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

Share Improve this question asked Dec 30, 2016 at 6:03 ManishDalwadiManishDalwadi 411 gold badge1 silver badge5 bronze badges 5
  • Have you looked at momnetjs? - momentjs.com – soundslikeodd Commented Dec 30, 2016 at 6:05
  • 1 reference: gist.github.com/tamboer/7316306 – dhruv jadia Commented Dec 30, 2016 at 6: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 Commented Dec 30, 2016 at 6:46
  • 1 Possible duplicate of Get the time difference between two datetimes – Akhil Mathew Commented Oct 5, 2017 at 9:25
  • Does this answer your question? How to convert minutes to hours/minutes and add various time values together using jQuery? – Michael Freidgeim Commented Feb 6, 2020 at 7:42
Add a comment  | 

4 Answers 4

Reset to default 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();

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.

One line is:

new Date(minutes * 60 * 1000).toISOString().substr(11, 8);

Just use this one line:

new Date(minutes * 60 * 1000).toISOString().substring(11, 19)

For example:

148 minutes = 02:28:00

发布评论

评论列表(0)

  1. 暂无评论