计算两个日期之间的百分比

时间:2017-07-28 11:24:51

标签: javascript php jquery

我想在两个日期之间计算差异与百分比。只有不是日期,需要数小时才能扩展。 示例:

22-08-2017 09:00 start date,
30.09.2017 22:00 finish date,

今天的日期是01.09.2017。当我今天看系统时,应用程序显示给我"%47%已完成"我想要这个。

5 个答案:

答案 0 :(得分:0)



function getpercent(){
var strt = new Date(document.getElementById('start').value).getTime();

var end = new Date(document.getElementById('end').value).getTime();

var current = new Date(document.getElementById('current').value).getTime();

var completed = ((current - strt) / (end - strt)) * 100;
document.getElementById('percent').innerHTML = completed+"%";
 }

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

<p>start <input id="start" type="date" /></p>
<p>end <input id="end" type="date" /></p>
<p>current <input id="current" type="date" /></p>
<p>percent   <span id="percent"></span></p>
<button onclick="getpercent()">get percent</button>
&#13;
&#13;
&#13;

new Date()将采用日期字符串并将其转换为unix标准时间秒。

答案 1 :(得分:0)

javascript Date对象可用于算术表达式。它将转换为毫秒。

var start, finish, midpoint, percent, elapsed;

start = new Date(2017, 8, 22, 9);
finish = new Date(2017, 9, 30, 22);
midpoint = new Date(2017, 8, 29, 12);

elapsed = midpoint - start;
percent = (elapsed / (finish - start)) * 100;

console.log('elapsed', elapsed, ' ms', percent, ' % complete');

https://jsfiddle.net/fwb3g4nc/1/

答案 2 :(得分:0)

这是一个简单的类DiffTracker,它在构造函数中接受两个日期 - startend日期,并且有一个方法getPercentage()将返回通过的小时数百分比从开始日期到startend日期之间的总小时数。

让我们看看以下情况:

开始日期= 2017-08-23 09:00

结束日期= 2017-08-24 09:00

所以现在小时的总差异 24

如果我们在2017-08-24 21:00致电getPercentage(),我们应该会看到 50%的结果。 如果我们在2017-08-24 03:00之前致电getPercentage(),我们应该看到 75%的结果,因为差异是18小时

&#13;
&#13;
var diffTraker = new DiffTracker(new Date(2017, 7, 23, 9), new Date(2017, 7, 24, 9));

console.log('Start Date');
console.log(new Date(2017, 7, 23, 9));
console.log('-------------------');
console.log('End Date');
console.log(new Date(2017, 7, 24, 9));
console.log('-------------------');
console.log(diffTraker.getPercentage(new Date(2017, 7, 23, 21)) + '% from start date (' + new Date(2017, 7, 23, 21) + ')');
console.log(diffTraker.getPercentage(new Date(2017, 7, 24, 3)) + '% from start date (' + new Date(2017, 7, 24, 3) + ')');
console.log(diffTraker.getPercentage(new Date(2017, 7, 24, 5)) + '% from start date (' + new Date(2017, 7, 24, 5) + ')');

function DiffTracker(startDate, endDate){
  var self = this;
  self.start = startDate;
  self.end = endDate;
  self.totalHours =  getDiffHours(self.start, self.end);
  
  self.getPercentage = function(date){
    var hoursFromStart = getDiffHours(self.start, date);
    return (hoursFromStart * 100 / self.totalHours).toFixed(2);
  }
  
  function getDiffHours(start, end){
    /* 36e5 is the scientific notation for 60*60*1000, dividing by which converts the milliseconds difference into hours */
    return Math.abs(start - end) / 36e5;
  }
}
&#13;
&#13;
&#13;

答案 3 :(得分:0)

在php中我使用这种方法:

<?php
$FirstDate = "22-08-2017 09:00";
$SecondDate = "30.09.2017 22:00";


$start    = strtotime($FirstDate); 
$finish   = strtotime($SecondDate);

$diff     = $finish - $start;

$progress = time() - $start; // You might have to modify the time function depending on where you live
$procent  = ($progress / $diff)*100;

$width    = round($procent);

// The if statment below just makes sure that it does not show 110% for example.

if($width >=100){
    echo "100 %";
}else{
    echo $width;
}

希望这有帮助!

答案 4 :(得分:0)

$ FirstDate =“ 22 -08-2017 09:00”; $ SecondDate =“ 23 .09.2017 22:00”;

即使有一天它显示100%