Search code examples
javascriptjquerydatepickertimepicker

air-datepicker js : hour+2


I try to set hours (+2h) to actual day and not for the others.

Example:

Today is October 18th, time: 10am.

  • If I click (without changing the date) -> Time is 12pm.
  • If I click and choose tomorrow (19th October) -> I choose any time.

Today is October 19th, time: 08am.

  • If I click (without changing the date) -> Time is 10am.
  • If I click and choose tomorrow (20th October) -> I choose any time.

PS : If possible, I want to disable the entry in the input (because we can change the time)

Thx

    var start = new Date()
	var hour = start.getHours()+2;

	
	$('#date').datepicker({
        language: 'en',
		timepicker: true,
		minHours: hour
	})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/air-datepicker/2.2.3/js/datepicker.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/air-datepicker/2.2.3/js/i18n/datepicker.en.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/air-datepicker/2.2.3/css/datepicker.css" rel="stylesheet"/>


<input type='text' id="date" class="" />


Solution

  • EDIT: I thought you want to disable previous dates (everything < now + 2hours). If not, just change minDate: start to startDate: start.

    Try with:

    var start = new Date();
    
    // first available date and time
    start.setHours(start.getHours() + 2);
    
    $('#date').datepicker({
      language: 'en',
      timepicker: true,
      minDate: start,
      onSelect: function(fd, d, picker) {
        if (d < start) {
          return;
        }
    
        if (d.getDate() == start.getDate() &&
          d.getMonth() == start.getMonth() &&
          d.getYear() == start.getYear()) {
          // set minHours to start hours
          picker.update({
            minHours: start.getHours()
          });
        } else {
          // set minHours to 0 hours
          picker.update({
            minHours: 0
          });
        }
      }
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
    <script src="https://cdnjs.cloudflare.com/ajax/libs/air-datepicker/2.2.3/js/datepicker.js"></script>
    
    <script src="https://cdnjs.cloudflare.com/ajax/libs/air-datepicker/2.2.3/js/i18n/datepicker.en.js"></script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/air-datepicker/2.2.3/css/datepicker.css" rel="stylesheet" />
    
    
    <input type='text' id="date" class="" />

    Also, air-timepicker docs: http://t1m0n.name/air-datepicker/docs/