Error generating date range with js

Asked

Viewed 40 times

0

I have the following code to take a date range and send it to my PHP function (which is ok now):

    function calcular(){
        var quantidade = document.getElementById('qnt').value;
        var inicio = document.getElementById('inicio').value;
        var fim = document.getElementById('fim').value;
        var total = (((Date.parse(fim)) - (Date.parse(inicio))) / (24 * 60 * 60 * 1000));
        total++;//para incluir o primeiro dia    
        document.getElementById('dias').innerHTML=(total)+" dias<br>";
        qtd = quantidade/total;
        document.getElementById('entrevistas').innerHTML=(qtd)+" entrevistas p/ dia<br>";
    
        inicio = new Date(Date.parse(inicio));    
    
        for(i=1;i<=total;i++)    {
            var dia = new Date();
            dia.setDate(inicio.getDate()+i);
            document.getElementById('resultado').innerHTML += "<input type='checkbox' checked onclick='redivide()' name='dias' value='"+dia+"'>"+(dia.getDate())+"/"+(dia.getMonth()+1)+"/"+dia.getFullYear()+"<br>";            
        }
       
        document.getElementById('resultado').innerHTML += "<input type='button' onclick='envia()' value='Salvar'><div id='resultado2'></div>";       
    
    }
    
    function redivide(){
        var checks = document.getElementsByName('dias');
        var total=0;
        for(i=0;i<checks.length;i++){
            if(checks[i].checked){
                total++;
            }
        }
        var quantidade = document.getElementById('qnt').value;
        document.getElementById('entrevistas').innerHTML=(quantidade/total)+" entrevistas p/ dia<br>";
        
        }
    
    function envia(){
        var checks = document.getElementsByName('dias');
        var vetor_datas = []; var j=0;
        for(i=0;i<checks.length;i++){
            if(checks[i].checked){
                vetor_datas[j] = checks[i].value;
                j++;
            }
        }
        document.getElementById('resultado2').innerHTML = vetor_datas;
    }
    <!DOCTYPE html>
    <html lang="pt-br">
    <head> 
       
       <meta charset="utf-8">
    </head>
    
    
    <body>
        Início <input type="date" id="inicio" value="2018-10-10">
        Fim <input type="date" id="fim" value="2018-10-15">
        Quantidade <input type='number' id="qnt" value='300'>
        <input type="button" value="Calcular" onclick="calcular()"><br>
        <div id='dias'></div>
        <div id='entrevistas'></div>
        <div id="resultado"></div>
    </body>
    </html>

My problem:

When I try to generate for an interval that starts on the first day of the month, it gives error in the script. I tried to fix here but could not. The interesting thing is that it works for the other dates (the beginning of the interval not being the first day of the month).

  • Can inform which error appears?

  • Actually it’s not a mistake, it would be the script that’s wrong. When I put, for example, 01/11/ it plays the first date for 01/12, but when I put 05/11 it plays normally for 05/12.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.