Decrease dates in javascript

-1

I'm trying to use moment.js but it's bone, see? !!!

I have 2 fields that return the date in PHP in the format dd / mm / yyyy. I want to show in a third field the difference between these dates. For example: 09/25/2018 and 09/30/2018 the response has to be 5 days. Obviously in PHP I have already done, however if the user changes a field these update in the third.

In the same example above, if you move from 25/09/2018 to 26/09/2018 auto-refresh in the third field.

I made a bag fiddle: link

    
asked by anonymous 08.10.2018 / 18:37

2 answers

4

So I understand, you calculate the difference in days between two dates, right?

Below is the change in the code quoted in the question:

var dtconclusao = document.getElementById("dtconclusao").value;
var dtfim = document.getElementById("dtfim").value;
var data1 = moment(dtconclusao, 'DD/MM/YYYY');
var data2 = moment(dtfim, 'DD/MM/YYYY');
var diferenca = data2.diff(data1, 'days');

alert(diferenca);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script><inputtype="hidden" id="dtconclusao" value="25/09/2018" />
<input type="hidden" id="dtfim" value="30/09/2018" />
    
08.10.2018 / 18:56
1

moment.min.js = > 51Kb

momentDoLeo.js = > 1Kb

Use momentDoLeo.js see operation

    //momentDoLeo.js

    var dtconclusao = document.getElementById("dtconclusao").value;
    dtconclusao = dtconclusao.split("/").reverse().join("-");

    var dtfim = document.getElementById("dtfim").value;
    dtfim = dtfim.split("/").reverse().join("-");

    var datafinal = new Date(dtfim);
    var datainicial = new Date(dtconclusao);

    var difDias = (parseInt((datafinal - datainicial) / (24 * 3600 * 1000)));
    
     document.getElementById("numeroDias").value=difDias;
<input type="hidden" id="dtconclusao" value="25/09/2018" />
<input type="hidden" id="dtfim" value="30/09/2018" />

<input type="text" id="numeroDias" name="numeroDias"/>
    
08.10.2018 / 20:00