Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Percentage between two dates compared to today

Tags:

javascript

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.

like image 281
SBB Avatar asked Apr 24 '15 05:04

SBB


People also ask

How do I calculate percentage between two dates?

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.

How do you find the percentage between 2 things?

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.

How do you calculate percent change per day?

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.


1 Answers

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/

like image 180
K K Avatar answered Oct 25 '22 08:10

K K