Created
March 12, 2012 09:39
-
-
Save olimortimer/2020935 to your computer and use it in GitHub Desktop.
JS: Disable next X working days & weekends
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* JS: Disable next X working days & weekends | |
* This disables selection of the next X working days in the jQuery datepicker | |
* along with weekends using the built in function | |
* | |
* 14/03/2012 - Oli Mortimer (olimortimer.com) | |
* Originally posted on StackOverflow by Junto (benpowell.co.uk) | |
* | |
* Usage: Change the required number of days in AddWeekDays(X) | |
**/ | |
var dateMin = new Date(); | |
var weekDays = AddWeekDays(3); | |
dateMin.setDate(dateMin.getDate() + weekDays); | |
function AddWeekDays(weekDaysToAdd) { | |
var daysToAdd = 0 | |
var mydate = new Date() | |
var day = mydate.getDay() | |
weekDaysToAdd = weekDaysToAdd - (5 - day) | |
if ((5 - day) < weekDaysToAdd || weekDaysToAdd == 1) { | |
daysToAdd = (5 - day) + 2 + daysToAdd | |
} else { // (5-day) >= weekDaysToAdd | |
daysToAdd = (5 - day) + daysToAdd | |
} | |
while (weekDaysToAdd != 0) { | |
var week = weekDaysToAdd - 5 | |
if (week > 0) { | |
daysToAdd = 7 + daysToAdd | |
weekDaysToAdd = weekDaysToAdd - 5 | |
} else { // week < 0 | |
daysToAdd = (5 + week) + daysToAdd | |
weekDaysToAdd = weekDaysToAdd - (5 + week) | |
} | |
} | |
return daysToAdd; | |
} | |
$('#datepicker').datepicker({ | |
beforeShowDay: $.datepicker.noWeekends, // disable weekends | |
minDate: dateMin | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment