如何计算时间之间的分钟数

时间:2017-04-21 01:35:58

标签: javascript time

我有开始时间和结束时间。分钟步骤是从开始时间到结束时间的15分钟。我想创建一个函数来计算开始时间和结束时间之间的分钟数。

function calculateNumberMinuteSteps(start_hour, start_minute, end_hour, end_minute, minute_step){
   // start time 8.15
   // end time 18.30
   // minute_step default 15
}

calculateNumberMinuteSteps(8, 15, 18, 30, 15); // Returns number of minute steps between 8.15am and 6.30pm

7 个答案:

答案 0 :(得分:1)

如果你回来,你会得到你想要的东西;

   return((end_hour-start_hour)*60 + (end_minute-start_minute))/minute_step;

答案 1 :(得分:0)

function calculateNumberMinuteSteps(start_hour, start_minute, end_hour, end_minute, minute_step) {
  const start_seconds = (start_hour * 60 * 60) + (start_minute * 60);
  const end_seconds = (end_hour * 60 * 60) + (end_minute * 60);
  const step_seconds = (minute_step * 60);
  return (end_seconds - start_seconds) / step_seconds;
}

console.log(calculateNumberMinuteSteps(8, 15, 18, 30, 15));

答案 2 :(得分:0)

计算起来非常容易。只需创建两个日期,差异就是以秒为单位的值。然后得到商。



function calculateNumberMinuteSteps(start_hour, start_minute, end_hour, end_minute, minute_step){
   // start time 8.15
   // end time 18.30
   // minute_step default 15
   minute_step === undefined?15:minute_step;
   var d1 = new Date("2000-01-01 " + start_hour + ":" + start_minute + ":00"),
       d2 = new Date("2000-01-01 " + end_hour + ":" + end_minute + ":00"),
       seconds =  (d2- d1)/1000, 
       step = Math.floor(seconds/(60*minute_step));
       return step;
}

calculateNumberMinuteSteps(8, 15, 18, 30, 15); // Returns number of minute steps between 8.15am and 6.30pm

console.log(calculateNumberMinuteSteps(8, 15, 18, 30, 15))




答案 3 :(得分:0)

以下内容将完全满足您的需求

function calculateNumberMinuteSteps(start_hour, start_minute, end_hour, end_minute, minute_step){
  var startTime = new Date(0, 0, 0, start_hour, start_minute)
  var endTime = new Date(0, 0, 0, end_hour, end_minute);
  var difference = endTime.getTime() - startTime.getTime();
  return steps difference/(minute_step * 60000);
}

var steps = calculateNumberMinuteSteps(8, 15, 18, 30, 15);

<强> Here is a working example

答案 4 :(得分:0)

(end_hour + end_minute / 60) - (start_hour + start_minute / 60)等于小时数的总小时数乘以60 / minute_step,返回总小时数乘以十五分钟的时间。

function calculateNumberMinuteSteps(sh = start_hour, sm = start_minute, eh = end_hour, em = end_minute, ms = minute_step, H = 60) {
   // start time 8.15
   // end time 18.30
   // minute_step default 15
   return ((eh + em / H) - (sh + sm / H)) * H / ms
}

var quarters = calculateNumberMinuteSteps(8, 15, 18, 30, 15); 

console.log(quarters);

答案 5 :(得分:0)

非常简单请见下文:

function calculateNumberMinuteSteps(start_hour, start_minute, end_hour, end_minute, minute_step){
  var startNumber = start_hour * 60 + start_minute;
  var endNumber = end_hour * 60 + end_minute;
  var totalSteps = (endNumber - startNumber) / minute_step;
  console.log(totalSteps);
}

calculateNumberMinuteSteps(8, 15, 18, 30, 15);

答案 6 :(得分:-1)

JS是一种camelCase语言,btw

function calculateNumberMinuteSteps(startTime, endTime, stepSize){
   return (endTime - startTime)/stepSize;
}

假设所有单位都是毫秒:)