Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable future dates after a week in input type = date?

I have an input box of type=date. I want it to disable all dates after one week from current date. Suppose current date is 11/07/2017, I want all dates from 18/07/2017 to be disabled. So the user can select only dates within one week from today. I've already disabled the past dates in my code.

function validDate(){
    var today = new Date().toISOString().split('T')[0];
    document.getElementsByName("date")[0].setAttribute('min', today);
}
<body onload="validDate()">
  <div class="form-group">
     <p>Date<span>*</span></p>
     <input type="date" name="date" id="date" class="form-control input-sm " required />
  </div>
</body>
like image 784
coder7777 Avatar asked Jul 11 '17 07:07

coder7777


People also ask

How do I restrict past dates in html5 input type date?

How do I turn off past dates? the previous dates we need to set the minDate property of the date picker. if we set minDate:0 then it will disable all the previous dates. and we set input attribute min:current_date then it will disable all the previous dates.

How do you turn off future date in date range picker?

daterangepicker({ autoUpdateInput: false, locale: { cancelLabel: 'Clear', format: 'DD-MM-YY' } });

How do I make my date field Disabled?

< p >To disable the date field, double click the "Disable" button.


4 Answers

Please see the updated code.

[after how many number of days to be disabled i.e. 6 in this case] * 24 * 60 * 60 * 1000

min and max attributes are used to set the minimum and maximum date for type="date"

function validDate(){
	    var today = new Date().toISOString().split('T')[0];
	    var nextWeekDate = new Date(new Date().getTime() + 6 * 24 * 60 * 60 * 1000).toISOString().split('T')[0]
      document.getElementsByName("date")[0].setAttribute('min', today);
      document.getElementsByName("date")[0].setAttribute('max', nextWeekDate)
	}
<body onload="validDate()">
<div class="form-group">
            <p>Date<span>*</span></p>
            <input type="date" name="date" id="date" class="form-control input-sm " required />
          </div>
</body>
like image 172
Lalit Sachdeva Avatar answered Nov 13 '22 07:11

Lalit Sachdeva


You can use 'max' to disable the future date:

var nextWeek = new Date();
nextWeek = nextWeek.setDate(nextWeek.getDate() + 1).toISOString().split('T')[0];
document.getElementsByName("date")[0].setAttribute('max', nextWeek);
like image 27
samAlvin Avatar answered Nov 13 '22 06:11

samAlvin


You can have something like

HTML

<table width="68%" border="0" align="center" cellpadding="0" cellspacing="6">

      <tr>
        <td align="left" valign="middle" class="search-filter-headings">Start Date:</td>
        <td align="left" valign="middle">
        <input type="text" name="start_date" id="start_date" class="filter-textfields" placeholder="Start Date"/>
        </td>
      </tr>
      <tr>
        <td align="left" valign="middle" class="search-filter-headings">End Date:</td>
        <td align="left" valign="middle">
        <input type="text" name="end_date" id="end_date" class="filter-textfields" placeholder="End Date"/>
        </td>
      </tr>

    </table>

JAVASCRIPT

    $( "#start_date" ).datepicker(

        { 
            maxDate: '0', 
            beforeShow : function()
            {
                jQuery( this ).datepicker({  maxDate: 0 });
            },
            altFormat: "dd/mm/yy", 
            dateFormat: 'dd/mm/yy'

        }

);

$( "#end_date" ).datepicker( 

        {
            maxDate: '7', 
            beforeShow : function()
            {
                jQuery( this ).datepicker('option','minDate', jQuery('#start_date').val() );
            } , 
            altFormat: "dd/mm/yy", 
            dateFormat: 'dd/mm/yy'

        }

);

Working Demo http://jsfiddle.net/X82aC/544/

Let me know if you require any further help

like image 39
Amaan Iqbal Avatar answered Nov 13 '22 05:11

Amaan Iqbal


You can use the max attribute of the date picker.

function getInputDateFormat(date) {
  return date.toISOString().split('T')[0];
}

function validDate() {
  var today = new Date();
  var maxDate = new Date();
  maxDate.setDate(maxDate.getDate() + 7);

  document.getElementsByName("date")[0].setAttribute('min', getInputDateFormat(today));
  document.getElementsByName("date")[0].setAttribute('max', getInputDateFormat(maxDate));
}
<body onload="validDate()">
  <div class="form-group">
    <p>Date<span>*</span></p>
    <input type="date" name="date" id="date" class="form-control input-sm " required />
  </div>
</body>
like image 1
H77 Avatar answered Nov 13 '22 05:11

H77