【问题标题】:Jquery Countup Until Next Monday and total Mondays from defined date until todays dateJquery Countup until Next Monday 和从定义日期到今天日期的总星期一
【发布时间】:2012-10-25 19:12:47
【问题描述】:

我正在尝试编写一个计数代码,以显示我们从特定日期开始有多少个星期一。

例如,自 2012 年 10 月 1 日起,我们有 4 个星期一到今天 10 月 25 日。

这第一步我可以通过从某些地方进行一些编码搜索并根据我的需要进行调整来实现。

现在对我来说棘手的部分是要找到一种方法来显示一个计数,该计数显示从现在到下个月剩余的百分比,以增加另一个星期一到主计数器。

例如,从 2012 年 1 月 1 日到现在,我们有 4 个星期一,但 10 月 25 日是星期四,那么我如何计算显示时间随着下星期一达到的剩余价值的增加而增加?

我尝试像使用天、小时、分钟和秒一样进行计数,因此每次运行时,它都会从今天开始计数,直到下周一为止,所以每周我们都会在counter: 6days 23hours 59minutes 59seconds 然后在星期一数上添加一个新数字,所以我们在 6 天后实现了一个新的星期一,但是当然,如​​果我访问运行中间代码的页面,我已经有了计数器增加的价值并计算每一秒的生活。

有人可以帮忙吗?如果您知道实现相同目标的更好方法,那完全没问题。

总结一下我想要实现的是一个计数器,它显示已经过去了多少个星期一以及下一个星期一还剩多少时间并实时计数。它必须永远持续下去,不能在下周一停止,在达到下周一之后它会一直持续到下周一。

欢迎提问!

<pre><code>


// This Code Counts how many mondays from a specified date to the actual date.
// Create a new date variable
var d = new Date();
var month = d.getMonth() + 1;
var day = d.getDate();

// Get the actual date and store on a variable
var actualDate = d.getFullYear() + '/' + (('' + month).length < 2 ? '0' : '') + month + '/' + (('' + day).length < 2 ? '0' : '') + day;

// Set dates start and get actual date
var date1 = new Date('10/01/2012'); // Set the begninning date here
var date2 = new Date(actualDate);

var monday = Math.abs(date1.getTime() - date2.getTime()) / (1000 * 60 * 60 * 24 * 7);
monday -= 0.1;
monday = Math.floor(monday);
monday *= 1;

if (date1.getDay() == 1) monday += 1;
if (date2.getDay() == 1) monday += 1;

//$('').html(monday);
$("#showMondays").text(monday);

</pre></code>

这是周一的工作柜台:http://jsfiddle.net/hRaAF/

谢谢!

【问题讨论】:

  • 您实际上想要达到的目标是否可能是向下到下周一?
  • 其实是相反的,应该是向上计数。例如,从日期 X 到今天,我们有 4 个星期一,我想显示从用户访问网站的时间到上个星期一已经过去了多少,以及我们离下一个星期一还有多远。因此,如果我们在一周的中间,我们应该在计数上显示一半的值(时间,如天时分秒)并显示它增加,以便在我们到达那里时完成另一个星期一......这就像多少我们仍然需要完成的百分比才能将另一个星期一添加到柜台......

标签: jquery count counter


【解决方案1】:

我将以下代码添加到您的小提琴中,您可以看到它正在运行here

var nextMonday = function(now) {
    var day = now.getUTCDay();
    var next = new Date();
    next.setDate(next.getDate() + (7 - day));
    next.setHours(0);
    next.setMinutes(0);
    next.setSeconds(0);
    return next;
};

var SECS = 1000;
var MINS = 60 * SECS;
var HOURS = 60 * MINS;
var DAYS = 24 * HOURS;

var countDown = function() {
    var now = new Date();
    var next = nextMonday(now);
    var difference = Math.abs(next - now);

    var days = Math.floor(difference / DAYS);
    difference = difference % DAYS;
    var hours = Math.floor(difference / HOURS);
    difference = difference % HOURS;
    var minutes = Math.floor(difference / MINS);
    difference = difference % MINS;
    var seconds = Math.floor(difference / SECS);

    var text = days + ' days ' + hours + ' hours ' + minutes + ' minutes ' + seconds + ' seconds ';

    $('#count').text(text);
    setTimeout(countDown, 1000);
};

countDown();
setTimeout(countDown, 1000);

编辑

Here是修改后的代码。我删除了以前计算自给定星期一以来的星期一数的代码,并替换为更简单的版本。您可以更改计算机中的日期和时间来测试它,因为这是 javascript 获取当前日期值的地方。

var begin = new Date('10/01/2012');

var SECS = 1000;
var MINS = 60 * SECS;
var HOURS = 60 * MINS;
var DAYS = 24 * HOURS;
var WEEKS = 7 * DAYS;

var countMondaysFrom = function(begin) {
    var difference = Math.abs(new Date() - begin);
    var days = Math.floor(difference / WEEKS);
    return days;

};

var lastMonday = function(now) {
    var day = now.getUTCDay() - 1;
    day = day < 0 ? 6 : day;
    var last = new Date();
    last.setDate(last.getDate() - day);
    last.setHours(0);
    last.setMinutes(0);
    last.setSeconds(0);
    return last;
};

var countUp = function() {
    var now = new Date();
    var last = lastMonday(now);
    var difference = Math.abs(now - last);

    var days = Math.floor(difference / DAYS);
    difference = difference % DAYS;
    var hours = Math.floor(difference / HOURS);
    difference = difference % HOURS;
    var minutes = Math.floor(difference / MINS);
    difference = difference % MINS;
    var seconds = Math.floor(difference / SECS);

    var text = days + ' days ' + hours + ' hours ' + minutes + ' minutes ' + seconds + ' seconds ';

    $('#count').text(text);
    $("#showMondays").text(countMondaysFrom(begin));
    setTimeout(countUp, 1000);
};

countUp();
setTimeout(countUp, 1000);​

【讨论】:

  • juan 我认为它有效,但由于某种原因,它似乎与天有一些问题,当我打开它时显示 5 天但今天是星期天,但如果我不考虑天数和只需将剩余的时间留到下周一就可以了。是否有可能做相反的事情,而不是倒计时,我们将秒、分钟、小时和天加起来直到下一个星期一,所以每次我们让计数器到达一个新的星期一时,它都会增加星期一计数器的值并重置数数继续前进……
猜你喜欢
  • 1970-01-01
  • 2023-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-06
  • 1970-01-01
  • 2019-03-03
相关资源
最近更新 更多