I am creating a UI which will contain Milestones and I will be using a progress bar to show this.
I am trying to come up with the percentage we are from the date the milestone is set to be completed vs today's date.
For example if I have a milestone called "Live to Site" that is set to happen on December 1, 2015, and I put the milestone in on Jan 1st, I want to determine the percentage we are from the start date to the end date.
Fiddle: http://jsfiddle.net/zz4c16fx/2/
var start = new Date(2015,0,1), // Jan 1, 2015 - The date we put this milestone in
end = new Date(2015,7,24), // June 24, 2015 - The date the milestone is due
today = new Date(), // April 23, 2015
p = Math.round(100-((end - start) * 100 ) / today) + '%';
// Update the progress bar
$('.bar').css( "width", p ).after().append(p);
That was my attempt at it but either my math is wrong or I am thinking about this incorrectly. The number its returning is 99%
. I feel that number should be lower seeing how we have 1.5 months left from today.
My end result is to show a progress bar that show how close we are to the completion date given the start, end and today's date.
Since subtracting two dates also yields the number of days between, the formula =(C2-A2)/(B2-A2) would result in the same percentage. The DATEDIF function makes it easy to calculate percentage of time elapsed.
Answer: To find the percentage of a number between two numbers, divide one number with the other and then multiply the result by 100. Let us see an example of finding the percentage of a number between two numbers.
Subtract the original value from the new value, then divide the result by the original value. Multiply the result by 100. The answer is the percent increase. Check your answer using the percentage increase calculator.
Try this:
var start = new Date(2015, 0, 1), // Jan 1, 2015
end = new Date(2015, 7, 24), // August 24, 2015
today = new Date(), // April 23, 2015
p = Math.round(((today - start) / (end - start)) * 100) + '%';
// Update the progress bar
$('.bar').css("width", p).after().append(p);
Demo: http://jsfiddle.net/zz4c16fx/6/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With