2017-11-15 1 views
0

안녕하세요 저는 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>

관련 문제