我有两个日期选择器。在第一个日期选择器中是开始日期,在第二个日期选择器中是结束日期。
我的年份范围是
2018-2019 (Last date is 31-03-2019)
2019-2020 (My start date is 01-04-2019 and the end date is 31-03-2020)
2020-2021 (My start date is 01-04-2020 and the end date is 31-03-2021)
//and so on
现在根据当前年份,我们处于2018-2019年的范围内。
所以我的结束日期是2019年3月31日。
如何显示31-03年的结束日期(取决于开始日期)?
我需要一些jquery或PHP来处理吗?
$(function() {
var year = (new Date).getFullYear();
$(".start_date").datepicker({
showOn: "button",
buttonImage: "http://jqueryui.com/resources/demos/datepicker/images/calendar.gif",
buttonImageOnly: true,
buttonText: "Select date",
dateFormat: "dd-mm-yy",
changeMonth: true,
changeYear: true,
minDate: 0,
//maxDate : "+1Y",
maxDate: new Date(year, 03, 31),
// maxDate: "+3m"
showAnim: "clip",
numberOfMonths: 1
});
$(".end_date").datepicker({
showOn: "button",
buttonImage: "http://jqueryui.com/resources/demos/datepicker/images/calendar.gif",
buttonImageOnly: true,
buttonText: "Select date",
dateFormat: "dd-mm-yy",
changeMonth: true,
changeYear: true,
minDate: 0,
//maxDate : "+1Y",
maxDate: new Date(year, 03, 31),
showAnim: "clip"
//numberOfMonths: 2
});
});
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<div class="form-group">
<label>Start Date</label>
<input type="text" name="start_date" placeholder="Start Date" id="start_date" class="start_date form-control">
</div>
<div class="form-group">
<label>End Date</label>
<input type="text" name="end_date" id="end_date" placeholder="End Date" class="end_date form-control">
</div>
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.3.1.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
您能帮我吗?
答案 0 :(得分:0)
您可以使用onSelect
选项在maxDate
日期选择器中设置.end_date
。
$(function() {
var year = (new Date).getFullYear();
$(".start_date").datepicker({
showOn: "button",
buttonImage: "http://jqueryui.com/resources/demos/datepicker/images/calendar.gif",
buttonImageOnly: true,
buttonText: "Select date",
dateFormat: "dd-mm-yy",
changeMonth: true,
changeYear: true,
minDate: 0,
//maxDate : "+1Y",
maxDate: new Date(year, 03, 31),
// maxDate: "+3m"
showAnim: "clip",
numberOfMonths: 1,
onSelect: function(dt, dp) {
var selected = $.datepicker.parseDate("dd-mm-yy", dt);
var yy = selected.getFullYear();
var mm = selected.getMonth();
var dd = selected.getDate()
var nextYear = $.datepicker.parseDate("dd-mm-yy", "31-03-" + (yy + 1));
$(".end_date").datepicker("option", "maxDate", nextYear);
}
});
$(".end_date").datepicker({
showOn: "button",
buttonImage: "http://jqueryui.com/resources/demos/datepicker/images/calendar.gif",
buttonImageOnly: true,
buttonText: "Select date",
dateFormat: "dd-mm-yy",
changeMonth: true,
changeYear: true,
minDate: 0,
//maxDate : "+1Y",
maxDate: new Date(year, 03, 31),
showAnim: "clip"
//numberOfMonths: 2
});
});
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<div class="form-group">
<label>Start Date</label>
<input type="text" name="start_date" placeholder="Start Date" id="start_date" class="start_date form-control">
</div>
<div class="form-group">
<label>End Date</label>
<input type="text" name="end_date" id="end_date" placeholder="End Date" class="end_date form-control">
</div>
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.3.1.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
由于我们以字符串形式获取日期,因此可以使用$.datepicker.parseDate()
将其解析为Date,并将年份递增为新的Date。
希望有帮助。