将倒计时时间改为13:30

时间:2016-01-19 11:02:13

标签: javascript jquery time

我想将我的倒计时修改为13:30,而不是16:00

<td>
    <strong>Order before 15:30</strong>
    <span><br>and your order is sent today</span>
</td>
$("strong:contains('Order before 15:30')").attr("id","countdown").delay(1000);

function ShowTime() {
    var now = new Date();
    var offset = now.getTimezoneOffset() / 60;

    now.setHours(now.getHours() + (offset+1));
    if (now.getHours() < 16) {
        var hrs = 15-now.getHours();
        var mins = 59-now.getMinutes();
        var secs = 59-now.getSeconds();

        if (hrs < 10) 
            hrs = "0" + hrs;

        if (mins < 10)     
            mins = "0" + mins;

        if (secs < 10) 
            secs = "0" + secs;

        timeLeft = 'Order within <span style="color: #ff713d; font-size: larger">' + hrs + 'h ' + mins + 'm ' + secs + 's</span>';
        $("#countdown").html(timeLeft);
        $("span:contains('and your order is sent today')").html("<br />and your package is sent today")
    }
}

var day = new Date().getDay(); 
if (day != 0 && day != 6) {
    setInterval(ShowTime, 1000);
}

JS Fiddle

我将如何实现这一目标?更改这些参数会返回错误吗?

var hrs = 13 - now.getHours();
var mins = 29 - now.getMinutes();
var secs = 29 - now.getSeconds();

1 个答案:

答案 0 :(得分:1)

function ShowTime(hour, min) {
    var now = new Date();
    var offset = now.getTimezoneOffset() / 60;

    now.setHours(now.getHours() + (offset+1));
    if (now.getHours() <= hour) {
        var hrs = ((hour-1)%24)-now.getHours();
        var mins = ((min-1)%60)-now.getMinutes();
        var secs = 59-now.getSeconds();

但是你需要对它进行一些改进,以确保它能正常工作,即使你的意思是&#34;&#34;第二天(现在是18:00,你想倒数第二天到第二天06:00)

您应该使用小时,分钟参数调用该函数:

setInterval(function(){ShowTime(16,00)}, 1000);