问题
I have a datepicker that has all past days disabled, and all days except Tuesdays and Wednesdays disabled.
I'd like the default day to be the frist enabled day of the week. If it's tuesday, then it should display tuesday, If wednesday, then wednesday. Any other day it should display next tuesday in the input field.
This is the code I'm using for the datepicker.
<script type="text/javascript">
$(document).ready(function(){
$( ".datepicker" ).datepicker({
minDate:'0',
changeMonth:true,
changeYear:true,
beforeShowDay:
function(dt)
{
return [dt.getDay() === 2 || dt.getDay() === 3, ""];
}});
});
</script>
And the input that I'm using the selector on:
<form method="POST">
<div style='margin:0 auto; text-align:center;'>
<label>Appointment Date: </label>
<input type="hidden" name="action" value="list_appointments" />
<input type="text" id="datepicker" name="date" class="datepicker" />
<!--value=--><?php #echo date_format(new DateTime("now",new DateTimeZone('America/Chicago')),'m/d/Y');?>
<input type="submit" value="Refresh List" name="Update" />
<br />
</div>
</form>
回答1:
You can use momentjs to make getting the next day of the week a little easier and then just have a if-else statement to get the correct day of the week:
function setDate() {
var today = moment().isoWeekday(); //or just = new Date().getDay();
if (today <= 2) {
return moment().isoWeekday(2);
} else if (today === 3) {
return moment().isoWeekday(3);
} else {
return moment().add(1, 'weeks').isoWeekday(2);
}
}
Call the setDate() function then and set that return value as the date in the datepicker.
$('.datepicker').datepicker("setDate", new Date(setDate()));
Here's a working example: https://jsfiddle.net/jutcw0ve/
来源:https://stackoverflow.com/questions/44836245/set-default-date-in-datepicker-with-tuesdays-wednesdays-only