【问题标题】:How to have a countdown timer to a date and time如何为日期和时间设置倒数计时器
【发布时间】:2015-07-13 11:35:42
【问题描述】:

我有这个代码:

<script>
    $(function () {
        var march04 = new Date();
        var march07 = new Date();
        var march11 = new Date();
        var march13 = new Date();
        var august09 = new Date();

        // march04 = new Date(march04.getFullYear() + 0, 3 - 1, 10);
        march07 = new Date(march07.getFullYear() + 0, 3 - 1, 8);
        march11 = new Date(march11.getFullYear() + 0, 3 - 1, 12);
        march13 = new Date(march13.getFullYear() + 0, 3 - 1, 14);
        august09 = new Date(august09.getFullYear() + 0, 8 - 1, 10);

        $('#march04').countdown({until: march04});
        $('#march07').countdown({until: march07});
        $('#march11').countdown({until: march11});
        $('#march13').countdown({until: march13});
        $('#august09').countdown({until: august09});

    });
</script>

目前它正在做的是计数到日期,但我也希望它倒计时到特定时间,例如 august09 19:00 pm。你能帮帮我吗?谢谢。

【问题讨论】:

  • 你在使用 rendro jquery 倒计时插件吗?如果是这种情况,请查看示例github.com/rendro/countdown/blob/master/examples/index.html
  • @DmitryK 我不认为我正在使用该插件,因为此代码是由另一个开发人员完成的。它使用的唯一代码在顶部,现在我被赋予了修改它并对日期和时间进行倒计时的任务。还有其他建议吗?

标签: jquery datetime countdown jquery-countdown


【解决方案1】:

您指定日期的方式只是完整的天数(即您不指定时间)。

你可以直接做

var march07_1900 = "March 7, 2015 19:00:00"; 

或者使用你的风格: 如果您不指定任何内容,则 Date() 将返回当前日期/时间。

全套参数如下所示:

new Date(year, month, day, hours, minutes, seconds, milliseconds) 

所以使用你的风格:

var march07_1900 = new Date();
march07_1900 = new Date(march07_1900.getFullYear() + 0, 3 - 1, 7, 19, 0, 0);
document.write(march07_1900);

然后你做

$('#march07').countdown({date: march07_1900});

【讨论】:

    【解决方案2】:

    我想你可能需要这个

    var today = new Date();
    var target = new Date("05-28-2015");
    var diff = new Date(target - today);
    var str = diff.getMonth() + ":" + diff.getDate() + " " + diff.getHours() + ":" + diff.getMinutes() + ":" + diff.getSeconds() + ":" + diff.getMilliseconds();

    Here 是一个示例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-22
      • 1970-01-01
      • 2021-02-03
      • 1970-01-01
      • 2022-01-05
      相关资源
      最近更新 更多