简体   繁体   English

Javascript比较两个日期有错误的结果

[英]Javascript comparing two dates has wrong result

I'm trying to learn a little more about JavaScript and decided to make a countdown timer that will show from years all the way down to milliseconds. 我试图学习更多有关JavaScript的知识,并决定制作一个倒计时计时器,该计时器将显示从数年一直到毫秒的时间。 It's just a learning experiment for me. 对我来说这只是一个学习实验。

The minutes are not correct. 分钟不正确。 If I refresh the browser, the seconds and minutes always start at 59. I think this may be because I am calling the Date object and possibly resetting it. 如果刷新浏览器,秒和分钟总是从59开始。我认为这可能是因为我正在调用Date对象并可能将其重置。 What I am looking for is to count down to a certain date. 我正在寻找的是倒数某个日期。

Because this is a learning experiment for me, if you see something else that may be improved upon, please let me know. 因为这是我的学习实验,所以如果您发现其他可以改进的地方,请告诉我。

var dateA = new Date();
var dateB = new Date('June 3, 2014 00:27:00');
var cntr  = setInterval(clock, 10);

function clock()
{
  dateB = (dateB - 10);
  var date = new Date(dateB);
  var yrs  = Math.abs(date.getUTCFullYear() - dateA.getUTCFullYear() );
  var mos  = Math.abs(date.getUTCMonth() - dateA.getUTCMonth());
  var days = Math.abs(date.getUTCDate() - dateA.getUTCDate());
  var hrs  = Math.abs(date.getUTCHours() - dateA.getUTCHours());
  var mins = Math.abs(date.getUTCMinutes() - dateA.getUTCMinutes());
  var secs = Math.ceil(date.getUTCSeconds() - dateA.getUTCSeconds() / 60);
  var mill = Math.ceil(date.getUTCMilliseconds() - dateA.getUTCMilliseconds() / 999);
  var str  =
    yrs  + ' Years   '  +
    mos  + ' Months   ' +
    days + ' Days   '   +
    hrs  + ' Hours   '  +
    mins + ' Mins   '   +
    secs + ' Secs   '   +
    mill + ' Mill';
  document.getElementById('clock').innerHTML = str;
}
 var yrs = Math.abs(date.getUTCFullYear() - dateA.getUTCFullYear() ); var mos = Math.abs(date.getUTCMonth() - dateA.getUTCMonth()); var days = Math.abs(date.getUTCDate() - dateA.getUTCDate()); var hrs = Math.abs(date.getUTCHours() - dateA.getUTCHours()); var mins = Math.abs(date.getUTCMinutes() - dateA.getUTCMinutes()); 

You cannot just take the absolute value of the differences of each part of the date! 您不能只取日期各部分差异的绝对值! You end up with totally wrong numbers. 您最终得到完全错误的数字。

 var secs = Math.ceil(date.getUTCSeconds() - dateA.getUTCSeconds() / 60); var mill = Math.ceil(date.getUTCMilliseconds() - dateA.getUTCMilliseconds() / 999); 

Why would you divide these by 60 and by nearly-1000?! 为什么将这些除以60,再除以近1000?

Instead, to calculate the time difference, you will need to get the complete difference (in milliseconds, usually) and convert that into the different units. 相反,要计算时差,您将需要获取完整的时差(通常以毫秒为单位)并将其转换为不同的单位。 Your function should look like this: 您的函数应如下所示:

var el = document.getElementById('clock');
function clock() {
  var diff = dateB - Date.now();
  var yrs  = Math.floor(diff / 31536000000);
  var mos  = Math.floor(diff / 2678400000) % 12;
  var days = Math.floor(diff / 86400000)   % 31;
  var hrs  = Math.floor(diff / 3600000)    % 24;
  var mins = Math.floor(diff / 60000)      % 60;
  var secs = Math.floor(diff / 1000)       % 60;
  var mill =            diff               % 1000;
  var str  =
    yrs  + ' Years   '  +
    mos  + ' Months   ' +
    days + ' Days   '   +
    hrs  + ' Hours   '  +
    mins + ' Mins   '   +
    secs + ' Secs   '   +
    mill + ' Mill';
  el.innerText = str;
}

If you're using javascript for comparing dates or counting number of days, you might have some problems. 如果您使用javascript比较日期或计算天数,则可能会遇到一些问题。 You should use a library for better results. 您应该使用一个库以获得更好的结果。

I recommend you to use http://momentjs.com/ for date or time function. 我建议您将http://momentjs.com/用于日期或时间功能。 It's easy to use and much more flexible. 它易于使用且更加灵活。

This should answer your question: Countdown timer using Moment js 这应该回答您的问题: 使用Moment js的倒数计时器

try this..

function checkFromDate(sender, args) {
    if (sender._selectedDate > new Date()) {
        alert("You cannot select a day future than today.");
        sender._selectedDate = new Date();
        sender._textbox.set_Value(sender._selectedDate.format(sender._format))
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM