In Datepicker i am trying to restrict selectable dates to sundays, and only if picked before 15:00 on the thursday. What am i doing wrong?, restricting to only sundays works, and cutoff time 15:00 works but the day before not two days before.
$( "#deliverysunday" ).datepicker({
dateFormat: "yy-mm-dd",
showWeek: true,
firstDay: 1,
beforeShowDay: function(date){ return [date.getDay() == 0,""]},
minDate: new Date().getHours() >= 15 ? 2 : 0
});
If I understand your problem correctly, you want to ensure that the earliest possible date to be picked is the first Sunday that occurs no later than Thursday at 15:00.
The minDate
option is going to control which date is the earliest selectable date; the logic to determine the cutoff time should go in your beforeShowDay
option:
$( "#deliverysunday" ).datepicker({ dateFormat: "yy-mm-dd", showWeek: true, firstDay: 1, beforeShowDay: function(date) { return [isDateSelectable(date)]; }, minDate: 2 }); /** * Given a date, returns a boolean to indicate * whether the date should be selectable in a picker. * Tests the date according to the rules: * * - Is the date a Sunday? * - Is it currently before the cutoff time? */ function isDateSelectable(date) { // Cutoff interval = Sun@00:00 - Thu@15:00 = 57 hours var intervalInMillis = 57 * 60 * 60 * 1000; var now = new Date(); var isPriorToInterval = now.getTime() < date.getTime() - intervalInMillis; // Is date a Sunday? var isSunday = date.getDay() === 0; return isSunday && isPriorToInterval; }
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/ui-lightness/jquery-ui.css"/> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="//code.jquery.com/ui/1.11.4/jquery-ui.min.js"></script> <h1>Date picker example</h1> <div id="deliverysunday"></div>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.