jQuery分两次获得比例

时间:2015-06-09 00:53:25

标签: javascript jquery

所以他们有这个公式来得到他们的比例。它是
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

1 个答案:

答案 0 :(得分:0)

由于总工作时间和持续时间均以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));