我想計算兩個日期之間的差異百分比。只有沒有日期,小時需要縮放。 例子:計算兩個日期之間的百分比
22-08-2017 09:00 start date,
30.09.2017 22:00 finish date,
的今天日期爲01.09.2017
。當我看到系統今天,應用程序顯示給我「%47%完成」我想這個。
我想計算兩個日期之間的差異百分比。只有沒有日期,小時需要縮放。 例子:計算兩個日期之間的百分比
22-08-2017 09:00 start date,
30.09.2017 22:00 finish date,
的今天日期爲01.09.2017
。當我看到系統今天,應用程序顯示給我「%47%完成」我想這個。
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>
新的Date()將日期字符串,並把它變成UNIX標準時間爲秒。
javascript日期對象可用於算術表達式。它會轉換爲毫秒。
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');
這是一個簡單的類DiffTracker
接受在構造函數中兩個日期 - start
和end
日期,有一個方法getPercentage()
,將返回從開始日期通過對總學時小時的百分比在start
和end
之間的日期。
讓我們來看看下面的場景:
開始日期= 2017年8月23日09:00
結束日期= 2017年8月24日09:00
所以,現在的總差小時
如果我們調用getPercentage()
與日期2017年8月24日21:00,我們應該看到的50%結果。 如果我們調用getPercentage()
與日期2017年8月24日03:00,我們應該看到的75%的結果,因爲不同的是18小時
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;
}
}
在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;
}
希望這會有所幫助!
$ FirstDate =「 -08-2017 09:00」; $ SecondDate =「 .09.2017 22:00」;
即使有一天它顯示100%
請給我們看一些代碼,你做的工作,我們會幫助。 – Grumpy