Calculate difference between two dates to validate date fields [duplicate]

3

I needed to find a mechanism to limit a search that never exceeded 3 months.

I have two dates, I want to validate if they are within the defined parameters.

I have this code () I'm using type date and datepicker to run on multiple browsers

  <script src="data/js/moment.js" type="text/javascript"></script>

                       <script type="text/javascript">
                    $("#pesquisa").on("submit", function (event) {
                        event.preventDefault();
                     var start= new Date(document.getElementById("datainicio").value);
                       var end = new Date(document.getElementById("datafim").value);
                    var dr    = moment.range(start, end);

                    dr.diff('months'); // 3
                    dr.diff('days'); // 92
                   alert( dr.diff();) // 7945200000

                  //   mostrarinfo();
            });
    
asked by anonymous 14.09.2015 / 11:01

3 answers

7

Good morning. Try this:

var date1 = new Date("7/11/2010");
var date2 = new Date("12/12/2010");
var timeDiff = Math.abs(date2.getTime() - date1.getTime());
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24)); 
alert(diffDays);

Test

Source: link

    
14.09.2015 / 13:32
4

No% of% dates are valued by the number of milliseconds from JavaScript . By subtracting a date from another you will have the difference between the dates in milliseconds.

The simplest way is to create the end date, based on the start date. I consider this path simpler because it does not involve a lot of calculation, since JavaScript always generates a valid date. So you can simply add 3 months to the start date without worrying about year-turn or leap year, for example.

Comparing end date with due date

var dataInicio = new Date(document.getElementById("datainicio").value);
var dataFim = new Date(document.getElementById("datafim").value);
var limiteFim = new Date(dataInicio.getFullYear(),
                         dataInicio.getMonth() + 3,
                         dataInicio.getDate());

return !(dataFim > limiteFim);

Calculating the difference between dates

var dataInicio = new Date(document.getElementById("datainicio").value);
var dataFim = new Date(document.getElementById("datafim").value);
var diffMilissegundos = dataFim - dataInicio;
var diffSegundos = diffMilissegundos / 1000;
var diffMinutos = diffSegundos / 60;
var diffHoras = diffMinutos / 60;
var diffDias = diffHoras / 24;
var diffMeses = diffDias / 30;

return !(diffMeses > 3);
    
14.09.2015 / 15:03
0

Simple and functional code. Use on multiple systems of mine:

$("#data-termino-ausencia").focusout(function(){
    var dtI = $(".data-inicio-ausencia").html().split('/');
    var dtF = $("#data-termino-ausencia").val().split('/');
    dia_I = dtI[0];
    dia_F = dtF[0];
    mes_I = dtI[1];
    mes_F = dtF[1];
    ano_I = dtI[2];
    ano_F = dtF[2];
    var calculoDia = dia_F - dia_I;
    var calculoMes = mes_F - mes_I;
    var calculoAno = ano_F - ano_I;

    if(calculoDia < "0" || calculoMes < "0" || calculoAno < "0"){
        BackOffice.alert("A data de término não pode ser anterior a data de inicio.", "Aviso", function(){
            $("#data-termino-ausencia").val("");
            $("#data-termino-ausencia").focus();
            BackOffice.closeModal('.modal', true);
        });
    } else if (calculoDia < "0" || calculoMes >= "0" || calculoAno >= "0") {       
        $(".outros-ausencia").focus();
    } else if (calculoDia <= "0" || calculoMes >= "0" || calculoAno >= "0") {
        $(".outros-ausencia").focus();<br>
    }
})
    
02.07.2017 / 07:43