Javascript日期对象月转换

时间:2017-06-27 23:33:23

标签: javascript date

我有一个日历小部件,用户可以在其中选择日期,并将当前时间与当天日期相比较。每当输入日期时,我都有一个小部分,其中innerHTML被更改为已用时间。但是,我的月份转换不起作用。当我在将来选择一个月时,例如八月。而不是输入2个月[x]天数,它只打印44天。我的代码出了什么问题?

function updateCountdown() {
   var dateToday = new Date();
   var dateFrom = Date.UTC(dateToday.getFullYear(), dateToday.getMonth(), dateToday.getDate(), dateToday.getHours(), dateToday.getMinutes(), dateToday.getSeconds());
   var dateTo = Date.UTC(dateObject.getFullYear(), dateObject.getMonth(), dateObject.getDate(), 0, 0, 0); // all launches at 8:00pm UTC

   if ((dateTo - dateFrom) < 1000) { // time will be less than 0 when setInterval runs next
      clearInterval(countdown);
      document.getElementById("countdownSection").style.display = "none";
   }

   //months
   var monthsUntil = Math.floor((dateTo - dateFrom) / 2880000);
   document.getElementById("countdown").innerHTML = " :" + monthsUntil + " months";

   // days
   var daysUntil = Math.floor((dateTo - dateFrom) / 86400000);
   document.getElementById("countdown").innerHTML = daysUntil + " days";

   // hours
   var fractionalDay = (dateTo - dateFrom) % 86400000;
   var hoursUntil = Math.floor(fractionalDay / 3600000);
   if (hoursUntil < 10) {
      hoursUntil = "0" + hoursUntil;
   }

   document.getElementById("countdown").innerHTML += " :" + hoursUntil + " hours";
}

1 个答案:

答案 0 :(得分:0)

更改

var dateTo = Date.UTC(dateObject.getFullYear(), dateObject.getMonth(), 
                      dateObject.getDate(), 0, 0, 0); // all launches at 8:00pm UTC

var dateTo = ... dateObject.getDate (), 19, 0, 0;