问题
I have a two date picker. In the first date picker is a start date and second date picker is an end date.
My year range is
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
Now according to the current year, we are in the range of 2018-2019.
So my end date will be the 31-03-2019.
How do I display the end date 31-03- year(depending upon the start_date)?
Should I need some jquery or PHP to handle this?
$(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>
Would you help me out in this?
回答1:
You can, using onSelect option, set the maxDate in the .end_date datepicker.
$(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>
Since we get the date in string, we can use $.datepicker.parseDate() to parse it into a Date and increment the year into a new Date.
Hope that helps.
来源:https://stackoverflow.com/questions/54265576/how-do-i-display-the-end-date-31-03-yeardepending-upon-the-start-date