簡體   English   中英

計算兩個日期之間的百分比

[英]Calculate between two dates to percent

我想計算兩個日期之間的差異百分比。 只是沒有日期,需要擴展的時間。 例子:

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

今天的日期是01.09.2017 當我今天查看系統時,應用程序向我顯示“%47 percent completed”我想要這個。

 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>

new Date() 將采用日期字符串並將其轉換為 unix 標准時間(以秒為單位)。

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/

這是一個簡單的DiffTracker類,它在構造函數中接受兩個日期 - start日期和end日期,並且有一個方法getPercentage()將返回從開始日期開始的小時數與start日期和end日期之間的總小時數的百分比。

讓我們看看以下場景:

開始日期 = 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 小時

 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 = " 22 -08-2017 09:00"; $SecondDate = " 23 .09.2017 22:00";

哪怕有一天顯示100%

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM