JS日期比较(阵列比较数组)不起作用

时间:2016-10-25 11:40:20

标签: javascript

这是我的代码,我从这里获取逻辑和一些片段:javascript - get array of dates between 2 dates - 首先,原型,只是用于下面的循环(:

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

其次,Holliday--主阵列 - 用来比较其他人:

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"));
...
}

还有两个函数,一个接收两个字符串并解析成新的Date对象并计算我有多少天,然后是星期六,周日和假日 - 现在,只测试假日,因为星期六和星期天正在工作细:

            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
                }
            };
        }  

第二个,确定日期是否为hollidays,包含在主数组中:

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

如果我在控制台个人日期比较上运行,就像这样,

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

工作正常,但在getDates(“2016-01-01”,“2016-12-31”)上有些日子被忽略了。 结果如下:

  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

但是,预计大约有12个结果,而不是6.我的代码有什么问题?

0 个答案:

没有答案