如何为日期和时间设置倒数计时器

How to have a countdown timer to a date and time

我有这个代码:

<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:00pm。你能帮帮我吗?谢谢

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

你可以直接做

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

或使用您的风格: 如果您未指定任何内容,则 Date() 将 return 当前 date/time.

全套参数如下所示:

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});

我想你可能需要这个

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是样本。