1

Thats my code, I took logic and some snippets from here: javascript - get array of dates between 2 dates - First, the prototype, just to use in a loop bellow(:

Date.prototype.addDays = function(days) {
                var dat = new Date(this.valueOf());
                dat.setDate(dat.getDate() + days);
                return dat;
            };

Second, Holliday - the main array - used to compare others:

var feriados = [];
/* Just pushing sume values into... */
function addFeriados(){
...
feriados.push(new Date("2016-01-01"));
feriados.push(new Date("2016-02-08"));
feriados.push(new Date("2016-02-09"));
feriados.push(new Date("2016-03-25"));
feriados.push(new Date("2016-04-21"));
feriados.push(new Date("2016-05-01"));
feriados.push(new Date("2016-05-26"));
feriados.push(new Date("2016-09-07"));
feriados.push(new Date("2016-10-12"));
feriados.push(new Date("2016-11-02"));
feriados.push(new Date("2016-11-15"));
feriados.push(new Date("2016-12-25"));
...
}

And two functions, one that recieve two strings and parse then into new Date objects and calculate how many days i have, and witch then are saturdays, sundays and hollidays - For now, just testing hollidays, because Saturdays and Sundays are working fine:

            function getDates(inicio, fim) {
            inicio = new Date(inicio);
            fim = new Date(fim);

            var dias = [] //All Days
            var uteis = []; //Working Days
            var fer = []; //Hollidays
            var sab = []; //Saturdays
            var dom = []; //Sundays
            var fds = []; //Weekends

            var atual = inicio;

            while (atual <= fim) {
                /* I've commented just to test the comparisons. */
                /*if(atual.getDay() == 0){
                    dom.push(atual);
                }
                else if(atual.getDay() == 6){
                    sab.push(atual);
                } Only this part will work - or, must be... */
                if(checkFeriado(atual)){
                    feriados.push(atual);
                }
                /*else{
                    uteis.push(atual);
                }
                dias.push(atual);*/
                atual = atual.addDays(1);
            }
            return {
                "total": dias,
                "uteis": uteis, 
                "feriados": feriados, 
                "fds": {
                    "sab": sab,
                    "dom": dom
                }
            };
        }  

And the second one, to determinate if date is a hollidays, contained in main array:

  function checkFeriado(data) {
        var i;
        for(i = 0; i < feriados.length; i++){
            if(feriados[i].getTime() == data.getTime()){
                return true;
            }
        }
        return false;
    }

If i run on console individual date comparison, like this,

checkFeriado(new Date("2016-04-21"));

Works fine, but on getDates("2016-01-01", "2016-12-31") some days are ommited. Here's the result:

  Date 2016-01-01T00:00:00.000Z
  Date 2016-02-08T00:00:00.000Z
  Date 2016-02-09T00:00:00.000Z
  Date 2016-11-02T00:00:00.000Z
  Date 2016-11-15T00:00:00.000Z
  Date 2016-12-25T00:00:00.000Z

But, it's expected about 12 results, not 6. Whats wrong with my code?

Community
  • 1
  • 1
Gabriel Vip
  • 83
  • 1
  • 5

0 Answers0