Doubt on how to calculate time difference with special conditions

1

I'm trying to figure out the time difference between two dates using Moment.js , but I need to look at some special conditions:

  

Case 1 : If you enter a day in a month, it counts as "a full month." Per   example: from day 31/09/2015 to 01/10/2015 = 2 months . In another example, from% with% to% with% = 4 months .

In this last example, by% w / w the result is 2 months (77 days). Here's a fiddle that forkei do posted by @Orion in comments.

Update 2: I think I've been able to solve this part a bit too much (to using 31/07/2015 and 16/10/2015 , then I call twice the same values ... gambiarra this né ???) follow the fiddle commented (I will not post the code here for now for the question does not get bigger yet, then post as an answer if a better / simpler solution does not appear) ... / update

This above is a calculation, and this below is another, unrelated (one is not a condition of the other, they are independent, although starting from the same principle - put a minimum limit of days to be considered a month) / p>

  

Case 2 : To count as a whole month you must have more than 15 days in the   month. For example: from day Moment.js to moment.js = 1 month (because only month enters 09, since September has 31 days and we always consider the   start day and end day. If it were until 10/15/2015 it would be two months). In another example, from split to 16/09/2015 = 14/10/2015 (as they did not reach 15 days, neither july nor october enter).

In PHP I got a lot of trouble, breaking the dates with 31/07/2015 and making 13/10/2015 to differentiate months of 30, 31 and 28 days. As in this case the period was not more than a year, then it worked. But here it has to be on the client ( or < jquery "), and the difference can take several years.

What I've done so far was the following:

function result(e) // pega os dados do form
{
    e.preventDefault();
    var a = document.getElementById("data1").value; // INÍCIO
    var b = document.getElementById("data2").value; // FIM

    // DIFERENÇA EM DIAS DIAS DO DIA INICIAL X FINAL 

    var checkin = moment(a, 'DD-MM-YYYY');
    var checkout = moment(b, 'DD-MM-YYYY');
    var meses = checkout.diff(checkin, 'months');

    if (meses > 0) {

        $('#meses').html(meses);
    }
}

The output here, with dates between 2 meses and explode is if's , and only if it completes an entire month it becomes 10/10/2010 (as of 10/10/2011).

How can I solve this to achieve both logics (with only one day, or at least 15), considering the "real" months (not the calendar month - 30 days)? It can be with or without a library ( 09/10/2011 or other). Thanks in advance.

    
asked by anonymous 27.08.2015 / 22:39

1 answer

2

Following is a possibility:

In case 1 it is very simple, just subtract the month of beginning of the month of term and add 1. In case 2 it is necessary to know how many days have the month of beginning, but there is a trick in JavaScript to know this value. / p>

var dataInicio = document.getElementById("dataInicio");
var dataTermino = document.getElementById("dataTermino");
var caso1 = document.getElementById("caso1");
var caso2 = document.getElementById("caso2");

var onInputChange = function (event) {
  var datas = {
    Inicio: new Date(dataInicio.value),
    Termino: new Date(dataTermino.value),
  }
  //verificando se ambas as datas são validas e a data de termino é posterior a de inicio.
  if (!isNaN(datas.Inicio) && !isNaN(datas.Termino) && datas.Termino > datas.Inicio) {
    var dtInicio = { 
      dia: datas.Inicio.getUTCDate(), 
      mes: datas.Inicio.getUTCMonth(),
      ano: datas.Inicio.getUTCFullYear()
    };
    var dtTermino = { 
      dia: datas.Termino.getUTCDate(), 
      mes: datas.Termino.getUTCMonth(),
      ano: datas.Termino.getUTCFullYear()
    };        
    dtInicio.totalDias = new Date(dtInicio.ano, dtInicio.mes + 1, 0);
    
    var qtdMesesCaso1 = dtTermino.mes - dtInicio.mes  + 1;
    qtdMesesCaso1 += (dtTermino.ano - dtInicio.ano) * 12
    
    var qtdMesesCaso2 = qtdMesesCaso1;
    if (dtInicio.totalDias < dtInicio.dia + 15)
      qtdMesesCaso2--;
    if (dtTermino.dia < 15)
      qtdMesesCaso2--;

    caso1.textContent = qtdMesesCaso1 + " Meses";
    caso2.textContent = qtdMesesCaso2 + " Meses";
  } else {
    caso1.textContent = "";
    caso2.textContent = "";
  }

}

dataInicio.addEventListener("change", onInputChange);
dataTermino.addEventListener("change", onInputChange);
<div>
    <label>
        Data Inicio:
        <input id="dataInicio" type="date" />
    </label>
</div>
<div>
    <label>
        Data Termino:
        <input id="dataTermino" type="date" />
    </label>
</div>
<div>
    Caso 1: <span id="caso1"></span>
</div>
<div>
    Caso 2: <span id="caso2"></span>
</div>
    
28.08.2015 / 18:49