实施开始日期和结束日期以查找日期差。如果我选择今天的开始日期和今天的结束日期,则差异显示为0,并且我也禁用了周末,尽管它也计算了周末。这是我的代码。 我试图解决的问题:
$("#startDate").datepicker({
beforeShowDay: $.datepicker.noWeekends,
changeMonth: true,
todayHighlight: true,
minDate: 0,
numberOfMonths: 1,
todayHighlight: true,
weekStart: 1,
weekEnd: 5,
onSelect: function(dateStr) {
var min = $(this).datepicker('getDate'); // Get selected date
$("#endDate").datepicker('option', 'minDate', min || '0'); // Set other min, default to today
}
});
$("#endDate").datepicker({
beforeShowDay: $.datepicker.noWeekends,
changeMonth: true,
endDate: true,
todayHighlight: true,
minDate: 0,
numberOfMonths: 1,
weekStart: 1,
weekEnd: 5,
buttonImageOnly: true,
onSelect: function(dateStr) {
var max = $(this).datepicker('getDate'); // Get selected date
$('#datepicker').datepicker('option', 'maxDate');
var start = $("#startDate").datepicker("getDate");
var end = $("#endDate").datepicker("getDate");
var days = (end - start) / (1000 * 60 * 60 * 24);
$("#noofDays").val(days);
}
});
<link href="css/jquery.ui.css" rel="stylesheet">
<script src="http://netdna.bootstrapcdn.com/bootstrap/3.1.0/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script src="js/jquery.validate.js"></script>
<div class="row">
<div class="col-sm-12 col-md-4">
<div class="form-group">
<label class="control-label col-md-12" style="padding-top: 1px;">Start Date<span class="text" style="color: red;">*</span></label>
<div class="col-sm-4 col-md-12">
<input type="text" class="form-control" name="startDate" id="startDate" placeholder="Start Date" required/>
</div>
</div>
</div>
<div class="col-sm-12 col-md-4">
<div class="form-group">
<label class="control-label col-md-12" style="padding-top: 1px;">End Date<span class="text" style="color: red;">*</span></label>
<div class="col-sm-4 col-md-12">
<input type="text" class="form-control" name="endDate" id="endDate" placeholder="End Date" required/>
</div>
</div>
</div>
<div class="col-sm-12 col-md-4">
<div class="form-group">
<label class="control-label col-md-12" style="padding-top: 1px;"> No of Days<span class="text" style="color: red;">*</span></label>
<div class="col-sm-4 col-md-12">
<input type="text" class="form-control" name="noofDays" id="noofDays" placeholder="No of days" required/>
</div>
</div>
</div>
</div>
答案 0 :(得分:0)
如果要同时包含开始日期和结束日期,则应在days
中添加一个,并且为了解决周末的问题,我对开始日期和结束日期之间的日期进行迭代,然后从天数(变量days
)(如果当前日期是周末)。
$("#startDate").datepicker({
beforeShowDay: $.datepicker.noWeekends,
changeMonth: true,
todayHighlight: true,
minDate: 0,
numberOfMonths: 1,
todayHighlight: true,
weekStart: 1,
weekEnd: 5,
onSelect: function(dateStr) {
var min = $(this).datepicker('getDate'); // Get selected date
$("#endDate").datepicker('option', 'minDate', min || '0'); // Set other min, default to today
}
});
$("#endDate").datepicker({
beforeShowDay: $.datepicker.noWeekends,
changeMonth: true,
endDate: true,
todayHighlight: true,
minDate: 0,
numberOfMonths: 1,
weekStart: 1,
weekEnd: 5,
buttonImageOnly: true,
onSelect: function(dateStr) {
var max = $(this).datepicker('getDate'); // Get selected date
$('#datepicker').datepicker('option', 'maxDate');
var start = $("#startDate").datepicker("getDate");
var end = $("#endDate").datepicker("getDate");
var days = ((end - start) / (1000 * 60 * 60 * 24)) + 1; // Add one to include the end day
for (var dayBetween = start; dayBetween <= end; dayBetween.setDate(dayBetween.getDate() + 1)) {
var dayOfWeek = dayBetween.getDay();
if (0 === dayOfWeek || 6 === dayOfWeek) { // Weekend day
days--;
}
}
$("#noofDays").val(days);
}
});
<link href="css/jquery.ui.css" rel="stylesheet">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script src="js/jquery.validate.js"></script>
<div class="row">
<div class="col-sm-12 col-md-4">
<div class="form-group">
<label class="control-label col-md-12" style="padding-top: 1px;">Start Date<span class="text" style="color: red;">*</span></label>
<div class="col-sm-4 col-md-12">
<input type="text" class="form-control" name="startDate" id="startDate" placeholder="Start Date" required/>
</div>
</div>
</div>
<div class="col-sm-12 col-md-4">
<div class="form-group">
<label class="control-label col-md-12" style="padding-top: 1px;">End Date<span class="text" style="color: red;">*</span></label>
<div class="col-sm-4 col-md-12">
<input type="text" class="form-control" name="endDate" id="endDate" placeholder="End Date" required/>
</div>
</div>
</div>
<div class="col-sm-12 col-md-4">
<div class="form-group">
<label class="control-label col-md-12" style="padding-top: 1px;"> No of Days<span class="text" style="color: red;">*</span></label>
<div class="col-sm-4 col-md-12">
<input type="text" class="form-control" name="noofDays" id="noofDays" placeholder="No of days" required/>
</div>
</div>
</div>
</div>