2017-05-24 7 views
0

私は少しのスクリプトが必要で、私はちょっと混乱しています。jQueryカウントダウンは今日または明日午前10時まで

私はこのプラグインを使用したい:http://keith-wood.name/countdown.html

目標:カウントダウンを持って、午前10時になりましたから数える - それは0-9だ場合:59:59午前あれば、今日時10まで数えます明日10時から10時までです。 それは分かりますか?

は、ここで私は(これは私が知っている、動作しません)ジャバスクリプト/ jQueryを使って必要なものです:

var currentDate = new Date(new Date().getTime()); 
var hours = currentDate.getHours(); 
var endTime; 
    if(hours >= 10){ 
     endTime = give me next day 10:00 
    } else { 
     endTime = give me this day 10:00 
    } 
$("#countdown").countdown({until: endTime, format: 'HMS'}); 
+0

でそれを見ることができ、それはhttps://stackoverflow.com/questions/1787939/check-time-difference-in-javascriptを助けるかもしれない、この質問をご確認ください –

答えて

2

(テスト目的のために追加されましたconsole.log()を)動作するはずです以下。 UTC時刻の代わりにのブラウザのタイムゾーンを使用することに注意してください。

var currentDate = new Date(new Date().getTime()); 
 
var hours = currentDate.getHours(); 
 
var endTime = new Date(currentDate); 
 
endTime.setMinutes(0); 
 
endTime.setSeconds(0); 
 
endTime.setHours(10); 
 

 
if(hours >= 10){ 
 
    endTime.setDate(endTime.getDate() + 1); 
 
} 
 
console.log(endTime); 
 
$("#countdown").countdown({until: endTime, format: 'HMS'});

+0

それは次の日のために働きます、現在の日には表示されません。 10時から12時に変更した場合、それ以上は動作しません... – MrLumbergh

+0

'endTime.setHours()'と 'hours> = 10'行の両方で10から12に変更してください。また、現在のタイムゾーンが使用されていることに注意してください。 – thepieterdc

+0

ああ!本当にありがとう。 – MrLumbergh

0

あなたが次の日に必要な場合は、その後、終了日を作成する(10静的)年、月、日、時間を渡し、現在の日付をインクリメントします。

$(function() { 
 
    var currentDate = new Date(); 
 
    var hours = currentDate.getHours(); 
 
    var day; 
 
    
 
    if(hours >= 10){ 
 
     day = currentDate.getDate() + 1; 
 
    } else { 
 
     day = currentDate.getDate(); 
 
    } 
 
    
 
    var endTime = new Date(currentDate.getFullYear(), currentDate.getMonth(), day, 10); 
 
    $("#countdown").countdown({until: endTime, format: 'HMS'}); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-countdown/2.0.2/jquery.plugin.min.js"></script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-countdown/2.0.2/jquery.countdown.min.js"></script> 
 
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jquery-countdown/2.0.2/jquery.countdown.min.css" /> 
 

 
<div id='countdown'></div>

0

あなたはそれをこのように扱うことができます。

currentDate.setHours(10,00,00); 
if(hours >= 10){ 
    endTime = currentDate.AddDays(1); 
} 
0

これは私が私のウェブサイトのための使用の機能です:dateパラメータで

function countDown(id, date = "Jan 5 2018") { 
    var int = setInterval(function() { 
    // Get todays date and time 
    var now = new Date().getTime(); 

    // Find the distance between now an the count down date 
    var distance = date - now; 

    // Time calculations for days, hours, minutes and seconds 
    var days = Math.floor(distance/(1000 * 60 * 60 * 24)); 
    var hours = Math.floor((distance % (1000 * 60 * 60 * 24))/(1000 * 60 * 60)); 
    var minutes = Math.floor((distance % (1000 * 60 * 60))/(1000 * 60)); 
    var seconds = Math.floor((distance % (1000 * 60))/1000); 

    // Display the result in the element with id="demo" 
    document.getElementById(id).innerHTML = days + "d " + hours + "h " 
              + minutes + "m " + seconds + "s "; 

    // If the count down is finished, write some text 
    if (distance < 0) { 
     clearInterval(x); 
     document.getElementById(id).innerHTML = "00d 00h 00m 00s"; 
    } 
    }, 1000); 

    return int; 
} 

、あなたの日付と時間を入力する必要があります(。例:2018年1月1日午後12時00分00秒) idのパラメータでは、idセレクタ('#myid'ではなく、名前は'myid')です。

私はこれが役に立つと願っています。

あなたは、アクションhere

関連する問題