Check whether the date range between two input’s is less than a month and apply a condition

Asked

Viewed 1,157 times

1

I have two input’s one for start date and one for end date, I need to create a condition on javascript(jquery) for case the range is less than one month, example: I will disable a button if the range 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 thank you.

UPDATE I managed using the tip from the friend below, I just made a few adjustments to fit my need Here

3 answers

0

0

This small 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 put an example here in Jsfiddle with your code, working the comparison of dates

https://jsfiddle.net/kk7peacz/1/

0


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

  • I was able to do it using your code, https://codepen.io/erickcouto/pen/VpNEjm. Mto thanks.

  • face all right, I know you’ve helped me, I tried to get the result I’d like but there’s something wrong not ta 100% http://codepen.io/erickcouto/pen/Vpnejm what I want is when I select a date less than 30 days the button per month is deactivated and when it is larger q 30 it is activated again, but when it is deactivated the graphic that appears has to be the por dia.

Browser other questions tagged

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