简体   繁体   中英

Calculate between two dates to percent

I want to calculate between two dates difference to percent. Only not date, hours needed to scale. Example:

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

The today date is 01.09.2017 . When I look to system today, the application show to me "%47 percent completed" I want to this.

 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() will take a date string and turn it into unix standard time as seconds.

The javascript Date object can be used in arithmetic expressions. It will convert to milliseconds.

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/

This is a simple class DiffTracker that accepts two dates in the constructor - start and end date and has one method getPercentage() that will return the percentage of hours passed from start date against the total hours between start and end date.

Let's see the following scenario:

Start Date = 2017-08-23 09:00

End Date = 2017-08-24 09:00

So now the total difference of hours is 24

If we call getPercentage() with date 2017-08-24 21:00 we should see result of 50% . If we call getPercentage() with date 2017-08-24 03:00 we should see result of 75% because the difference is 18 hours

 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; } }

In php i use this method:

<?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;
}

Hope this helps!

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

Even if one day it shows 100%

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM