DatePicker set two dates with startDate >= tomorrow

女生的网名这么多〃 提交于 2021-01-29 02:49:57

问题


I'm trying to build 2 datepickers with the following restriction:

  1. startDate >= tomorrow
  2. endDate > startDate
  3. endDate can't select a date if startDate not selected

just like the checkin / checkout hotel rooms. I'm using this so far

function customRange(input) 
{ 
    return {
     minDate: (input.id == "startDate" ? new Date(2008, 12 - 1, 1) : null),
     minDate: (input.id == "endDate" ? $("#startDate").datepicker("getDate") : null), 
     maxDate: (input.id == "startDate" ? $("#endDate").datepicker("getDate") : null)
   }; 
}

Can someone help me out in here?

Thanks


回答1:


Try this

1) First get today's and tomorrow's dates.

var today = new Date();
var tomorrow = new Date(today.getTime() + 24 * 60 * 60 * 1000);

2) Set tomorrow's date as minDate in start Date.

minDate: tomorrow

3) When you select a start date, set it as minDate for end Date.

onSelect: function (dat, inst) {
        $('#end').datepicker('option', 'minDate', dat);
    }

The entire thing is going to handle with 3 options in Datepicker.

1. minDate  
2. maxDate  (if needed)
3. onSelect

$('#end').datepicker();
var today = new Date();
var tomorrow = new Date(today.getTime() + 24 * 60 * 60 * 1000);
console.log(tomorrow);
$('#start').datepicker({
    minDate: tomorrow,
    onSelect: function (dat, inst) {
        $('#end').datepicker('option', 'minDate', dat);
    }
});

JSFiddle

Hope you understand.



来源:https://stackoverflow.com/questions/20371800/datepicker-set-two-dates-with-startdate-tomorrow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!