Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate percentage between two dates in JavaScript

Tags:

javascript

I've been trying to calculate the percentage between two dates given the code from some questions here in StackOverflow but the code doesn't work correctly. This is the question: Get the percent of time elapsed between two javascript dates

var start = new Date(2015,6,1),
end = new Date(2015,12,1),
today = new Date();

alert(Math.round(( ( today - start ) / ( end - start ) ) * 100) + "%");

I want to calculate the progress from the 1rst of june to the 1rst of december and I get "-7"

Please help! Thanks

like image 324
Cristian Tirado Avatar asked Jun 19 '15 04:06

Cristian Tirado


1 Answers

 var start = new Date(2015,6,1),
 end = new Date(2015,11,1),
 today = new Date();

 //use Math.abs to avoid sign
 var q = Math.abs(today-start);
 var d = Math.abs(end-start);
 alert("Rounded: "+Math.round((q/d)*100) + "%");
 alert("Fraction: "+((q/d)*100) + "%");

Also read Ray Toal answer 6 is july not jun

like image 64
Bhupendra Piprava Avatar answered Sep 20 '22 23:09

Bhupendra Piprava