Percentage between two dates compared to today
I am creating a UI that will contain breakpoints and I will use a progress bar to show this.
I am trying to find the percentage we are doing from the date this milestone is completed versus today's date.
For example, if I have a milestone called "Live to Site" due on December 1st, 2015, and I set the milestone on January 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);
This was my attempt, but either my math is wrong or I am thinking about it the wrong way. Returned number 99%
. I feel that the number should be lower as we have 1.5 months left.
My end result is to show a progress bar that shows how close we are to the end date given the start date, end date and today.
source to share
Try this, convert end and start date to timestamp values.
// Convert to unix values timestamp values
var startDate = start.getTime();
var endDate = end.getTime();
var todayDate = today.getTime();
// Get the total possible timestamp value
var total = endDate - startDate;
// Get the current value
var current = todayDate - startDate;
// Get the percentage
var percentage = (current / total) * 100;
alert(percentage);
JsFiddle http://jsfiddle.net/x3snbz2w/2/
source to share
To get the percentage of elapsed days between a date and a date, you need to calculate the time between start and end, and then calculate the time between start and today. Then divide the second number by the first:
var start = new Date(2015,0,1),
end = new Date(2015,7,24),
today = new Date();
var timeBetweenStartAndEnd = (end - start);
var timeBetweenStartAndToday = (today - start);
var p = Math.round(timeBetweenStartAndToday / timeBetweenStartAndEnd * 100);
console.log("Percentage of days elapsed thus far: " + p);
source to share