Search code examples
javascriptdatewhile-loopconditional-statements

counting leap years between 2 dates using conditional statements & while loops JavaScript


I have a larger function that is calculating the interval of time between an input year, month and day and now in JavaScript. In this part of my function, I'm testing to see which year is larger (because the way it's designed, it can take a date either from the past or in the future and can calculate the interval of time)... In the beginning, I calculated the interval of time in UTC then converted it to days and eventually I have to return the interval in years, months and days again so convert the interval again. In this section, I'm trying to get an accurate count of how many leap years occurred during the time span but because the input date could be in the future or past, I've set up conditional statements to see which date is bigger so I can iterate through the timespan. Anyways, i'm having alot of problems creating this function (which is just a small part of my larger function) to test for leap year and add to the leap year count (which I want returned so I can accurately convert the interval in days to years, months and days). I feel like my syntax with all of these conditional statements is a little off (I really just started programming in JavaScript a couple months ago) & I don't know how to assign the variable "year" in the leap year algorithm test before that test so I can test it since I want year to be all the years in between the 2 years (yearNow and yearThen)... Anyways, Any help would be greatly appreciated:

var leapYear = function (inputYear, inputMonth, inputDay) {
  var now = new Date();
  var yearNow= now.getFullYear();
  var then = new Date(inputYear, (inputMonth-1), inputDay);
  var yearThen = then.getFullYear;
  var leapYearCount = 0;
  var beginYear = 0;
  if (yearNow < yearThen)
  beginYear = yearNow;
  while (beginYear < yearThen) {
  if ((year & 3) === 0 && ((year % 25) !== 0 || (year & 15) === 0))
    leapYearCount += 1;
    beginYear++;
  }
  if (yearThen < yearNow)
  beginYear = yearThen;
  while (beginYear < yearNow) {
  if ((year & 3) === 0 && ((year % 25) !== 0 || (year & 15) === 0))
    leapYearCount += 1;
    beginYear++;
  }
  return leapYearCount;
};

Edit** I tried setting what's here as "year" as "beginYear" but ended up getting 489 leap years between 2005 and now which is definitely Not correct- I feel like I might be doing something else Wrong in this function besides just not assigning "year" but I'm not sure what... Any help would be much appreciated.


Solution

  • This will count your leapyears, keep in mind it ll count the current year also as leap year if its a leap year and also the starting year as a leap year if its a leap year.

        var countLeapYears = function(y,m,d){
            var yearNow = new Date().getFullYear(),
                yearThen = y,
                beginYear = 0,
                endYear = 0,
                leapYearCount = 0;
      
            var isLeapYear = function(year){
              return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
            }
      
            if(yearNow < y){
               beginYear = yearNow;
               endYear = y;
            }else if(yearNow > y){
               beginYear = y;
               endYear = yearNow;
            }else if(yearNow == y){
               beginYear = y;
               endYear = y;
            }
     
            for(i = beginYear; i <= endYear; i++){
              if(isLeapYear(i)){
                leapYearCount++;
              }
            }
      
            return leapYearCount;
        }
    
        console.log(countLeapYears(2005,10,12)); // 3
        console.log(countLeapYears(2020,10,12)); // 2
        console.log(countLeapYears(2016,10,12)); // 1