What's new

Help Question for bot

jay41

Forum Veteran
Joined
Nov 13, 2016
Posts
1,928
Reaction
472
Points
658
Age
24
How to max of 3months the between days of datefrom and dateto.

Here is my code

$('#datefrom').datepicker({
format: "yyyy-mm-dd",
autoclose: true,
todayHighlight: true
}).on('changeDate', function (selected) {
var startDate = new Date(selected.date.valueOf());
$('#dateto').datepicker('setStartDate', startDate);
});

$('#dateto').datepicker({
format: "yyyy-mm-dd",
autoclose: true,
todayHighlight: true
}).on('changeDate', function (selected) {
var endDate = new Date(selected.date.valueOf());
$('#datefrom').datepicker('setEndDate', endDate);
});
 
To limit the range between the "datefrom" and "dateto" inputs to a maximum of 3 months, you can modify your code as follows:

JavaScript:
$('#datefrom').datepicker({
    format: "yyyy-mm-dd",
    autoclose: true,
    todayHighlight: true
}).on('changeDate', function (selected) {
    var startDate = new Date(selected.date.valueOf());
    var endDate = new Date(startDate);
    endDate.setMonth(endDate.getMonth() + 3); // Set the maximum end date to 3 months from the selected start date
    $('#dateto').datepicker('setStartDate', startDate);
    $('#dateto').datepicker('setEndDate', endDate);
});

$('#dateto').datepicker({
    format: "yyyy-mm-dd",
    autoclose: true,
    todayHighlight: true
}).on('changeDate', function (selected) {
    var endDate = new Date(selected.date.valueOf());
    $('#datefrom').datepicker('setEndDate', endDate);
});

In this code, we added a new variable endDate which is set to the selected start date plus 3 months. Then, we set this endDate as the maximum end date for the "dateto" input using the setEndDate method. This ensures that the range between the two inputs is limited to a maximum of 3 months.

Note: Make sure you have the appropriate datepicker library included in your code for the datepicker functionality to work properly.
 

Similar threads

Back
Top