最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Get the percent of time elapsed between two javascript dates - Stack Overflow

programmeradmin3浏览0评论

I'm trying to find how many days are left in a school year and return it as a jQuery UI progressbar.

jQuery UI progressbars only take percentages. How can I find the percentage of how far along I am in the timespan between two supplied dates, given today's date?

I'm trying to find how many days are left in a school year and return it as a jQuery UI progressbar.

jQuery UI progressbars only take percentages. How can I find the percentage of how far along I am in the timespan between two supplied dates, given today's date?

Share Improve this question edited Mar 16, 2016 at 1:29 Eric Leschinski 154k96 gold badges422 silver badges337 bronze badges asked Jan 12, 2011 at 18:44 KyleKyle 411 silver badge2 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 9

Example: http://jsfiddle/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 wanted the percentage remaining:

Example: http://jsfiddle/FLaJM/3/

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

If you are using MomentJS, which I highly remend for Javascript date stuff, you could 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...

pleted = ((timenow - timestart) / (timeclose - timestart)) * 100

The easiest way (algorithmically - you can figure out the code as an exercise) is to:

  1. Determine the number of days you have in the school year (typically ~185 for most public schools)
  2. Calculate the number of school days left between your current date and the end of classes (make sure you ignore weekends, holidays, in-service days, etc as these are not part of the 185).
  3. Take the value from step two and divide it into the first. That will give you your percentage.
  4. Display that in the jQuery UI progress bar.
发布评论

评论列表(0)

  1. 暂无评论