2017-11-15 8 views
0

こんにちは、こんにちは、こんにちは、私はここNewで、JavaScriptで動作することはありません。 だから、多少私を助けてくれるかもしれません。 日付からカウントアップするアップカウンターを作りたいと思います。 すべて正常に動作しますが、カウンタを月に追加したいとします。ここでは、コードは以下のとおりです。アップカウンターのJavascriptでの日付

window.onload=function() { 
 
    // Month,Day,Year,Hour,Minute,Second 
 
    upTime('jan,01,2013,00:00:00'); // ****** Change this line! 
 
}; 
 
function upTime(countTo) { 
 
    now = new Date(); 
 
    countTo = new Date(countTo); 
 
    difference = (now-countTo); 
 
    days=Math.floor(difference/(60*60*1000*24)*1); 
 
    years = Math.floor(days/365); 
 
    if (years > 1){ days = days - (years * 365)} 
 
    hours=Math.floor((difference%(60*60*1000*24))/(60*60*1000)*1); 
 
    mins=Math.floor(((difference%(60*60*1000*24))%(60*60*1000))/(60*1000)*1); 
 
    secs=Math.floor((((difference%(60*60*1000*24))%(60*60*1000))%(60*1000))/1000*1); 
 
    document.getElementById('years').firstChild.nodeValue = years; 
 
    document.getElementById('days').firstChild.nodeValue = days; 
 
    document.getElementById('hours').firstChild.nodeValue = hours; 
 
    document.getElementById('minutes').firstChild.nodeValue = mins; 
 
    document.getElementById('seconds').firstChild.nodeValue = secs; 
 

 
    clearTimeout(upTime.to); 
 
    upTime.to=setTimeout(function(){ upTime(countTo); },1000); 
 
}
<div id="countup"> 
 
    It's been 
 
    <p id="years">00</p> 
 
    <p class="timeRefYears">years</p> 
 
    <p id="days">00</p> 
 
    <p class="timeRefDays">days</p> 
 
    <p id="hours">00</p> 
 
    <p class="timeRefHours">hours</p> 
 
    <p id="minutes">00</p> 
 
    <p class="timeRefMinutes">minutes</p> 
 
    <p id="seconds">00</p> 
 
    <p class="timeRefSeconds">second</p> 
 
</div>

たぶん、あなたは私を助けることができると私は理解しようとしています。 は

答えて

0

あなたのスクリプトが容易かつ確実に正確ヶ月はこれを見て持って

を追加されませできませんありがとうございます。

私は十分にテストしていないので、調整が必要な場合があります。

それはあなた

window.onload = function() { 
 
    // Month,Day,Year,Hour,Minute,Second 
 
    upTime('jan,16,2013,19:30:00'); // ****** Change this line! 
 
}; 
 

 
function upTime(countTo) { 
 
    var now = new Date(), countTo = new Date(countTo), 
 
    years = now.getFullYear()-countTo.getFullYear(),  
 
    months = now.getMonth() -countTo.getMonth(), 
 
    days = now.getDate() -countTo.getDate(), 
 
    hours = now.getHours() -countTo.getHours(), 
 
    mins = now.getMinutes() -countTo.getMinutes(),  
 
    secs = now.getSeconds() -countTo.getSeconds();  
 
    if (months<0) { 
 
    years--; 
 
    months += 12; 
 
    } 
 
    if (days<0) { 
 
    months--; 
 
    var daysOfMonth = new Date(now.getTime()) 
 
    daysOfMonth.setMonth(now.getMonth()+1) 
 
    daysOfMonth.setDate(0); 
 
    days+=daysOfMonth.getDate(); 
 
    } 
 
    if (hours<0) { 
 
    days--; 
 
    hours+=24; 
 
    } 
 
    if (mins<0) { 
 
    hours--; 
 
    mins+=60; 
 
    } 
 
    if (secs<0) { 
 
    mins--; 
 
    secs+=60; 
 
    } 
 
    if (months<0) months = 0; 
 
    
 

 
    document.getElementById('years' ).innerHTML = years; 
 
    document.getElementById('months').innerHTML = months; 
 
    document.getElementById('days' ).innerHTML = days; 
 
    document.getElementById('hours' ).innerHTML = hours; 
 
    document.getElementById('minutes').innerHTML = mins; 
 
    document.getElementById('seconds').innerHTML = secs; 
 

 
    clearTimeout(upTime.to); 
 
    upTime.to = setTimeout(function() { 
 
    upTime(countTo); 
 
    }, 1000); 
 
}
<div id="countup"> 
 
    It's been 
 
    <p id="years">00</p> 
 
    <p class="timeRefYears">years</p> 
 
    <p id="months">00</p> 
 
    <p class="timeRefMonths">months</p> 
 
    <p id="days">00</p> 
 
    <p class="timeRefDays">days</p> 
 
    <p id="hours">00</p> 
 
    <p class="timeRefHours">hours</p> 
 
    <p id="minutes">00</p> 
 
    <p class="timeRefMinutes">minutes</p> 
 
    <p id="seconds">00</p> 
 
    <p class="timeRefSeconds">seconds</p> 
 
</div>

よりも正確でなければなりません動作します