使用JavaScript计算日期计时器

时间:2016-06-13 05:47:10

标签: javascript date timer counter leap-year

我想知道如何获得这种时间格式..我想要一个日期计数定时器就像链接上的这个...

任何答案或建议将不胜感激。谢谢,期待您的回复。

I want it to be Like This.

Js Fiddle

$(function(){
var calcNewYear = setInterval(function(){
    date_started = new Date("June 15, 2008");
    date_now = new Date();'

    seconds = Math.floor((date_now - (date_started))/1000);
    minutes = Math.floor(seconds/60);
    hours = Math.floor(minutes/60);
    days = Math.floor(hours/24);
    years = Math.floor(days/365);


    days = days -(years*365);
    hours = hours-(years*365*24)-(days*24);
    minutes = minutes-(years*365*24*60)-(days*24*60)-(hours*60);
    seconds = seconds-(years*365*24*60*60)-(days*24*60*60)-(hours*60*60)-(minutes*60);

    $("#mission").text( years + "Years |" + days + "Days |" + hours + "Hours |" + seconds + "Seconds");
},1000);

});

2 个答案:

答案 0 :(得分:0)

对于闰年 - 使用模数选项来检查它是否为4的倍数,然后使用if条件并将365天更改为366 JsFiddle以供参考 - https://jsfiddle.net/Nagasai_Aytha/2f30178y/2/

          if (years % 4 == 0) {
          days = days - (years * 366);
          hours = hours - (years * 366 * 24) - (days * 24);
          minutes = minutes - (years * 366 * 24 * 60) - (days * 24 * 60) - (hours * 60);
          seconds = seconds - (years * 366 * 24 * 60 * 60) - (days * 24 * 60 * 60) - (hours * 60 * 60) - (minutes * 60);

      } else {


          days = days - (years * 365);
          hours = hours - (years * 365 * 24) - (days * 24);
          minutes = minutes - (years * 365 * 24 * 60) - (days * 24 * 60) - (hours * 60);
          seconds = seconds - (years * 365 * 24 * 60 * 60) - (days * 24 * 60 * 60) - (hours * 60 * 60) - (minutes * 60);
      }

答案 1 :(得分:0)

我已经尝试过此代码。希望它可以帮助其他遇到我同样问题的人。

epsilon