Javascript中的向上计数器来自日期

时间:2017-11-15 15:33:33

标签: 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>
&#13;
&#13;
&#13;

也许你可以帮助我,我正在努力理解。 谢谢

1 个答案:

答案 0 :(得分: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>