Get the percentage of time elapsed between two javascript dates

I am trying to find how many days are left in the school year and return it as an indicator of jQuery progress.

jQuery UI progressbars only processes percentages. How can I find a percentage of how far I am between the two dates indicated today?

+4
source share
3 answers

Example: http://jsfiddle.net/FLaJM/4/

var start = new Date(2005,0,1), end = new Date(2021,0,1), today = new Date(); alert( Math.round(100-((end - start) * 100 ) / today) + '%' ); 

or if you need the remaining percentage:

Example: http://jsfiddle.net/FLaJM/3/

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

If you are using MomentJS , which I highly recommend for Javascript material, you can do this:

 var percentOfDayRangeComplete = function(start, end) { var now = moment(); start = start || moment(now).startOf('day'); end = end || moment(now).endOf('day'); var totalMillisInRange = end.valueOf() - start.valueOf(); var elapsedMillis = now.valueOf() - start.valueOf(); // This will bound the number to 0 and 100 return Math.max(0, Math.min(100, 100 * (elapsedMillis / totalMillisInRange))); }; 

jsFiddle to see it in action ...

+1
source

The easiest way (algorithmically - you can define the code as an exercise):

  • Determine the number of days you have in the school year (usually ~ 185 for most public schools).
  • Calculate the number of school days left between your current date and the end of classes (make sure you ignore weekends, holidays, work days, etc., since they are not part of 185).
  • Take the value from step two and divide it into the first. This will give you a percentage.
  • Display this in the jQuery UI progress bar.
0
source

Source: https://habr.com/ru/post/1335288/


All Articles