You are viewing limited content. For full access, please sign in.

Question

Question

Date Range on a Form

asked on September 9, 2016

I am looking to have a start date and end date on a form where the end date can not be more than 6 weeks after the start date. Is there a way to make this happen using JavaScript?

0 0

Replies

replied on September 11, 2016

You can use following JavaScript:

$(document).ready(function () {
  $(".startdate input").change(function(){
	 setMaxEndDate();
});
 
  function setMaxEndDate(){
    var startdate=$('.startdate input').val();
    //var parts=startdate.split("/"); //this works when date format is dd/MM/yyyy
    //var d = new Date(parts[2],parts[1]-1,parts[0]);
    var d= new Date(startdate); //this works for other date format such as m/d/yyyy
    d.setMonth(d.getMonth()+6);
    var year=d.getFullYear();
    var month=("0"+(d.getMonth()+1)).slice(-2);
    var day=("0"+d.getDate()).slice(-2);
    var maxDate=(year+"-"+month+"-"+day);
    $('.enddate input').attr('max',maxDate);
  }
});

 

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.