Verify that the date range between two input's is less than one month and apply a condition

1

I have two input's one for start date and one for end date, I need to create a condition in javascript (jquery) if the interval is less than a month, eg: I'll disable a button if the interval is less than 1 month.

Follow my current code: Here.

Note: I could not make the code work right here in the post, if someone can edit it, thank you.

UPDATE I've been using the friend's tip below, I've only made some adjustments to fit my need Here

    
asked by anonymous 05.04.2017 / 18:54

3 answers

1

Javascript

function Testar() {
var data_inicial = document.getElementById("dataInicial").value;
var data_final = document.getElementById("dataFinal").value;

var date1 = new Date(data_inicial);
var date2 = new Date(data_final);
var timeDiff = Math.abs(date2.getTime() - date1.getTime());
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24)); 
	if(diffDays <= 30)
	{
	alert("Menor que 30 dias - botão 'Testar' vai ser desabilitado");
	document.getElementById("demo").innerHTML = "";
	}
};
  <label class="fa fa-calendar">
        <input id="dataInicial" name="dataInicial" type="text" value="">
  </label>
  <label class="fa fa-calendar">
        <input id="dataFinal" name="dataFinal" type="text" value="">
  </label>
  <div id="demo"><input name='Salvar' type='submit' id='Salvar' value='Testar' onclick='Testar()'></div>

OBS; dates in mm / dd / yyyy format

    
05.04.2017 / 20:05
1

Friend, quick script, I think it fits what you need:

link

    
05.04.2017 / 19:11
1

This little script will return the difference in DAYS of your two dates.

var dataIni = new Date($('#startDate').val());
var dataFim = new Date($('#endDate').val());
var diferencaEmMili = Math.abs(dataFim.getTime() - dataIni.getTime());
var diferencaEmDias = Math.ceil(diferencaEmMili / (1000 * 3600 * 24)); 

I've put an example here in JSFiddle with your code, working date comparison

link

    
05.04.2017 / 19:17