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

后端 未结 10 801
旧巷少年郎
旧巷少年郎 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:32

    I took the liberty of modifying ConorLuddy's answer to address both 24 hour time and 12 hour time.

    function minutesToHHMM (mins, twentyFour = false) {
      let h = Math.floor(mins / 60);
      let m = mins % 60;
      m = m < 10 ? '0' + m : m;
    
      if (twentyFour) {
        h = h < 10 ? '0' + h : h;
        return `${h}:${m}`;
      } else {
        let a = 'am';
        if (h >= 12) a = 'pm';
        if (h > 12) h = h - 12;
        return `${h}:${m} ${a}`;
      }
    }
    
    0 讨论(0)
  • 2020-12-07 13:33
    function parseMinutes(x) {
      hours = Math.floor(x / 60);
      minutes = x % 60;
    }
    
    function parseHours(H, M) {
      x = M + H * 60;
    }
    
    0 讨论(0)
  • 2020-12-07 13:36

    In case you want to return a string in 00:00 format...

    convertMinsToHrsMins: function (minutes) {
      var h = Math.floor(minutes / 60);
      var m = minutes % 60;
      h = h < 10 ? '0' + h : h;
      m = m < 10 ? '0' + m : m;
      return h + ':' + m;
    }
    

    Thanks for the up-votes. Here's a slightly tidier ES6 version :)

    function convertMinsToHrsMins(mins) {
      let h = Math.floor(mins / 60);
      let m = mins % 60;
      h = h < 10 ? '0' + h : h;
      m = m < 10 ? '0' + m : m;
      return `${h}:${m}`;
    }
    
    0 讨论(0)
  • 2020-12-07 13:36

    This code can be used with timezone

    javascript:

    let minToHm = (m) => {
      let h = Math.floor(m / 60);
      h += (h < 0) ? 1 : 0;
      let m2 = Math.abs(m % 60);
      m2 = (m2 < 10) ? '0' + m2 : m2;
      return (h < 0 ? '' : '+') + h + ':' + m2;
    }
    
    console.log(minToHm(210)) // "+3:30"
    console.log(minToHm(-210)) // "-3:30"
    console.log(minToHm(0)) // "+0:00"

    minToHm(210)
    "+3:30"
    
    minToHm(-210)
    "-3:30"
    
    minToHm(0)
    "+0:00"
    
    0 讨论(0)
  • 2020-12-07 13:43

    Not a jquery plugin, but the DateJS Library appears to do what you require. The Getting Started page has a number of examples.

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

    Q1:

    $(document).ready(function() {
        var totalMinutes = $('.totalMin').html();
    
        var hours = Math.floor(totalMinutes / 60);          
        var minutes = totalMinutes % 60;
    
        $('.convertedHour').html(hours);
        $('.convertedMin').html(minutes);    
    });
    

    Q2:

    $(document).ready(function() {
    
        var minutes = 0;
    
        $('.min').each(function() {
            minutes = parseInt($(this).html()) + minutes;
        });
    
        var realmin = minutes % 60
        var hours = Math.floor(minutes / 60)
    
        $('.hour').each(function() {
            hours = parseInt($(this).html()) + hours;
        });
    
    
        $('.totalHour').html(hours);
        $('.totalMin').html(realmin);    
    
    });
    
    0 讨论(0)
提交回复
热议问题