Skip to content Skip to sidebar Skip to footer

Percentage Between Two Dates Compared To Today

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 s

Solution 1:

Try this:

var start = newDate(2015, 0, 1), // Jan 1, 2015
    end = newDate(2015, 7, 24), // August 24, 2015
    today = newDate(), // 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/

Solution 2:

Try this, convert the end and start date to unit timestamp values.

// Convert to unix values timestamp valuesvar startDate = start.getTime();
var endDate = end.getTime();
var todayDate = today.getTime();

// Get the total possible timestamp valuevar total = endDate - startDate;

// Get the current valuevar current = todayDate - startDate;

// Get the percentagevar percentage = (current / total) * 100;

alert(percentage);

JsFiddle http://jsfiddle.net/x3snbz2w/2/

Solution 3:

To get the percentage of days elapsed thus far between one date and another, you need to calculate the time between start and end, then calculate the time between start and today. Then divide the second number by the first:

var start = newDate(2015,0,1),
    end = newDate(2015,7,24), 
    today = newDate();

var timeBetweenStartAndEnd = (end - start); 
var timeBetweenStartAndToday = (today - start);

var p = Math.round(timeBetweenStartAndToday / timeBetweenStartAndEnd * 100); 

console.log("Percentage of days elapsed thus far: " + p);

Post a Comment for "Percentage Between Two Dates Compared To Today"