How to convert minutes to hours/minutes and add various time values together using jQuery?

后端 未结 10 802
旧巷少年郎
旧巷少年郎 2020-12-07 13:03

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 d

相关标签:
10条回答
  • 2020-12-07 13:48

    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.

    function display(a){
      var hours = Math.trunc(a/60);
      var minutes = a % 60;
      console.log(hours +":"+ minutes);
    }
    
    display(120); //"2:0"
    display(60); //"1:0:
    display(100); //"1:40"
    display(126); //"2:6"
    display(45); //"0:45"
    
    0 讨论(0)
  • 2020-12-07 13:48

    0 讨论(0)
  • 2020-12-07 13:50
    var timeConvert = function(n){
     var minutes = n%60
     var hours = (n - minutes) / 60
     console.log(hours + ":" + minutes)
    }
    timeConvert(65)
    

    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...

    0 讨论(0)
  • 2020-12-07 13:51

    Alternated to support older browsers.

    function minutesToHHMM (mins, twentyFour) {
      var h = Math.floor(mins / 60);
      var m = mins % 60;
      m = m < 10 ? '0' + m : m;
    
      if (twentyFour === 'EU') {
        h = h < 10 ? '0' + h : h;
        return h+':'+m;
      } else {
        var a = 'am';
        if (h >= 12) a = 'pm';
        if (h > 12) h = h - 12;
        return h+':'+m+a;
      }
    }
    
    0 讨论(0)
提交回复
热议问题