2

I'm trying to set up a counter to determine the difference between a set past date and the current date.

I have managed to set up a counter to determine the seconds between the two points, splitting the results into years, days, hours, minutes, seconds using the following code:

var lastDay = new Date("Jan 1, 1994  00:00:01").getTime(); 
var x = setInterval(function() { 
var now = new Date().getTime(); 
var t = now - lastDay; 
var years = Math.floor(t / (1000 * 60 * 60 * 24)/ 365);
var days = Math.floor((t % (1000 * 60 * 60 * 24 * 365))/(1000 * 60 * 60 * 24)); 
var hours = Math.floor((t % (1000 * 60 * 60 * 24))/(1000 * 60 * 60)); 
var minutes = Math.floor((t % (1000 * 60 * 60)) / (1000 * 60)); 
var seconds = Math.floor((t % (1000 * 60)) / 1000); 
document.getElementById("year").innerHTML =years ; 
document.getElementById("day").innerHTML =days ; 
document.getElementById("hour").innerHTML =hours; 
document.getElementById("minute").innerHTML = minutes;  
document.getElementById("second").innerHTML =seconds;  
if (t < 0) { 
        clearInterval(x); 
        document.getElementById("demo").innerHTML = "TIME UP"; 
        document.getElementById("year").innerHTML ='0'; 
        document.getElementById("day").innerHTML ='0'; 
        document.getElementById("hour").innerHTML ='0'; 
        document.getElementById("minute").innerHTML ='0' ;  
        document.getElementById("second").innerHTML = '0'; } 
}, 1000);

The problem I have with this, is that it does not factor in leap years, therefore the 'days' figure is inaccurate. It should add another 7 days to account for the number of leap years between the set date and the current date (at time of writing).

I have tried to use the following code below to count the leap years:

var countLeapYears = function(){
  var yearNow = new Date().getFullYear(); 
    var then = new Date("Jan 1, 1994 00:00:01");
    var yearThen = then.getFullYear();
    var beginYear = 0;
    var endYear = 0;
    var leapYearCount = 0;

    var isLeapYear = function(year){
      return ((year % 4 === 0) && (year % 100 !== 0)) || (year % 400 === 0);
    }

    if(yearNow < y){
       beginYear = yearNow;
       endYear = yearThen;
    }else if(yearNow > yearThen){
       beginYear = yearThen;
       endYear = yearNow;
    }else if(yearNow == yearThen){
       beginYear = yearThen;
       endYear = yearThen;
    }

    for(i = beginYear; i <= endYear; i++){
      if(isLeapYear(i)){
        leapYearCount++;
      }
    }

    return leapYearCount;
}

I then tried to add the 'leapYearCount' to the 'days' but it failed:

var countLeapYears = function(){
  var yearNow = new Date().getFullYear(); 
    var then = new Date("Jan 1, 1994 00:00:01");
    var yearThen = then.getFullYear();
    var beginYear = 0;
    var endYear = 0;
    var leapYearCount = 0;

    var isLeapYear = function(year){
      return ((year % 4 === 0) && (year % 100 !== 0)) || (year % 400 === 0);
    }

    if(yearNow < y){
       beginYear = yearNow;
       endYear = yearThen;
    }else if(yearNow > yearThen){
       beginYear = yearThen;
       endYear = yearNow;
    }else if(yearNow == yearThen){
       beginYear = yearThen;
       endYear = yearThen;
    }

    for(i = beginYear; i <= endYear; i++){
      if(isLeapYear(i)){
        leapYearCount++;
      }
    }

    return leapYearCount;
}

var lastDay = new Date("Jan 1, 1994 00:00:01").getTime(); 
var x = setInterval(function() { 
var now = new Date().getTime(); 
var t = now - lastDay; 
var years = Math.floor(t / (1000 * 60 * 60 * 24)/ 365);
var days = Math.floor((t % (1000 * 60 * 60 * 24 * 365))/(1000 * 60 * 60 * 24) + leapYearCount); 
var hours = Math.floor((t % (1000 * 60 * 60 * 24))/(1000 * 60 * 60)); 
var minutes = Math.floor((t % (1000 * 60 * 60)) / (1000 * 60)); 
var seconds = Math.floor((t % (1000 * 60)) / 1000); 
document.getElementById("year").innerHTML =years ; 
document.getElementById("day").innerHTML =days ; 
document.getElementById("hour").innerHTML =hours; 
document.getElementById("minute").innerHTML = minutes;  
document.getElementById("second").innerHTML =seconds;  
if (t < 0) { 
        clearInterval(x); 
        document.getElementById("demo").innerHTML = "TIME UP"; 
        document.getElementById("year").innerHTML ='0'; 
        document.getElementById("day").innerHTML ='0'; 
        document.getElementById("hour").innerHTML ='0'; 
        document.getElementById("minute").innerHTML ='0' ;  
        document.getElementById("second").innerHTML = '0'; } 
}, 1000);

Any ideas how I can correct this and add the extra days to account for the number of leap years which have passed?

Many thanks.

Chris
  • 21
  • 1
  • There are already many, many questions on this, e.g. [*Difference between two dates in years, months, days in JavaScript*](https://stackoverflow.com/questions/17732897/difference-between-two-dates-in-years-months-days-in-javascript) and [*How to get the difference of two dates in mm-dd-hh format in Javascript*](https://stackoverflow.com/questions/35504942/how-to-get-the-difference-of-two-dates-in-mm-dd-hh-format-in-javascript) and [this answer](https://stackoverflow.com/a/24890372/257182). – RobG Feb 02 '21 at 22:42
  • Many questions, but none that address this question specifically. Hence asking the question with more detail and some example code. I just needed to check what I had. The answers in the linked questions didn't work either. Thanks anyway. – Chris Feb 04 '21 at 21:20
  • If your specific question is "how to determine the number of leap years between two dates", that is a different question to the question title and opening statement. The algorithm `floor(years ÷ 4) + 1)` doesn't work accurately in many cases, e.g. 1 Mar 1996 to 28 Feb 2004 contains 1 leap day but the algorithm returns 3. – RobG Feb 04 '21 at 23:50
  • Read the whole post. The question is very clear – Chris Feb 05 '21 at 21:17

1 Answers1

0

The answer was a lot simpler than I had previously thought.

As I was just trying to factor in the difference in days because of the leap year, I just had to divide the number of years by 4 and add 1, like so:

var lastDay = new Date("Jan 1, 1994 10:00:00").getTime(); 
var x = setInterval(function() { 
var now = new Date().getTime(); 
var t = now - lastDay; 
var years = Math.floor(t / (1000 * 60 * 60 * 24)/ 365);
var leapDays = Math.floor((years / 4) + 1);
var days = Math.floor((t % (1000 * 60 * 60 * 24 * 365))/(1000 * 60 * 60 * 24) - leapDays); 
var hours = Math.floor((t % (1000 * 60 * 60 * 24))/(1000 * 60 * 60)); 
var minutes = Math.floor((t % (1000 * 60 * 60)) / (1000 * 60)); 
var seconds = Math.floor((t % (1000 * 60)) / 1000); 
document.getElementById("year").innerHTML =years ; 
document.getElementById("day").innerHTML =days ; 
document.getElementById("hour").innerHTML =hours; 
document.getElementById("minute").innerHTML = minutes;  
document.getElementById("second").innerHTML =seconds;  
if (t < 0) { 
        clearInterval(x); 
        document.getElementById("demo").innerHTML = "TIME UP"; 
        document.getElementById("year").innerHTML ='0'; 
        document.getElementById("day").innerHTML ='0'; 
        document.getElementById("hour").innerHTML ='0'; 
        document.getElementById("minute").innerHTML ='0' ;  
        document.getElementById("second").innerHTML = '0'; } 
}, 1000);
Chris
  • 21
  • 1
  • The above just throws errors in Safari. The algorithm for leap years is faulty. For the period 1 March 1896 to 28 February 1904 it returns 2 extra days, when there are zero leap years in that period. `(Math.floor((new Date(1904,1,28)-new Date(1896,2,1))/8.64e7/365)/4|0)+1`. Similarly, the period 1 Mar 1996 to 28 Feb 2004 contains 1 leap year, but the algorithm returns 3. – RobG Feb 04 '21 at 23:47
  • I'm running this on both safari and chrome with no issues. The answers are correct with every variation of the date I've tried. – Chris Feb 05 '21 at 21:17