因此,他们有此公式来获取比率。它是
total working hours = time-start - time-end
ratio = total working hours / duration
我可以使用以下代码获取total working hours



var timestart = '11:32:59 AM';
var timeend = '5:24:04 PM';
var duration = '00:44:08';

var totalworkinghours = timemod3(timestart, timeend);
console.log(totalworkinghours);

function timemod3(start, end) { //(H:MM:SS AM/PM) // GET THE DIFFERENCE OF TWO TIME
  //get the 24 hour format of start time
  Number.prototype.padDigit = function() {
    return (this < 10) ? '0' + this : this;
  }
  var ssplit = start.split(':');
  //var hour = ssplit[0];
  //var minutes = ssplit[1];
  var ss2 = ssplit[2].split(' ');
  var ampm1 = ss2[1]; //start

  var ssplit2 = end.split(':');
  //var hour2 = ssplit2[0];
  //var minutes2 = ssplit[1];
  var ss3 = ssplit2[2].split(' ');
  var ampm2 = ss3[1]; //end

  var st = convertTo24hr(start);
  var et = convertTo24hr(end);
  /************************************/
  var st1;
  var en;
  if (ampm1 == 'AM' && ampm2 == "PM") {
    //if sched is morning
    st1 = et;
    en = st;
  } else if (ampm1 == 'PM' && ampm2 == "AM") {
    //if sched is evening
    st1 = st;
    en = et;
  }
  var s = st1.split(':');
  var e = en.split(':');
  var se = Number(s[2]) - Number(e[2]);
  var sems = Math.floor(parseInt(se / 60));
  var mi = Number(s[1]) - Number(e[1]) - sems;
  var mihr = Math.floor(parseInt(mi / 60));
  var hr = Number(s[0]) - Number(e[0]) - mihr;

  if (se < 0) {
    mi = mi - 1;
    se = se + 60;
  }
  if (mi < 0) {
    hr = hr - 1;
    mi = mi + 60;
  }
  if (hr < 0) {
    //se = 0;
    //mi = 0;
    hr = 0;
  }
  var result = hr.padDigit() + ':' + mi.padDigit() + ':' + se.padDigit();
  if (result == 'NaN:NaN:' + se) {
    result = '00:00:00';
  }
  return result;
}

function convertTo24hr(time) {
  var t = time.split(':');
  var hours = Number(t[0]);
  var minutes = Number(t[1]);
  var t2 = t[2].split(' ');
  var sec = t2[0];
  var AMPM = t2[1];
  if ((AMPM == "pm" || AMPM == "PM") && hours < 12) {
    hours = hours + 12;
  }
  if ((AMPM == "am" || AMPM == "AM") && hours == 12) {
    hours = hours - 12;
  }
  var sHours = hours.toString();
  var sMinutes = minutes.toString();
  if (hours < 10) sHours = "0" + sHours;
  if (minutes < 10) sMinutes = "0" + sMinutes;
  return (sHours + ':' + sMinutes + ':' + sec);
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>





但是我不知道如何得到这个比例。帮我得到比例。顺便说一句,它在excel中的结果是7.95506向上取整使其变为8,因此比率为8

最佳答案

由于总工作时间和工时均以xx:xx:xx时间格式给出,因此在除以比率之前将时间转换为秒。

var myTotalHours = '05:51:05';
var myDuration =  '00:44:08';

function determineRatio(totalHours, duration) {

    var totalHoursArray = totalHours.split(':');
    var durationArray = duration.split(':');

    var numerator = parseInt(totalHoursArray[0])*3600
        + parseInt(totalHoursArray[1])*60
        + parseInt(totalHoursArray[2]);
    var denominator = parseInt(durationArray[0])*3600
        + parseInt(durationArray[1])*60
        + parseInt(durationArray[2]);

    return Math.round(numerator/denominator);

}

alert(determineRatio(myTotalHours,myDuration));

关于javascript - jQuery两次除法得到比率,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30721368/

10-11 12:01