❓ Help Question for bot

jay41

Forum Veteran
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

About this Thread

  • 1
    Replies
  • 235
    Views
  • 1
    Participants
Last reply from:
Unknown user

Online now

Members online
995
Guests online
1,037
Total visitors
2,032

Forum statistics

Threads
2,276,172
Posts
28,968,039
Members
1,231,153
Latest member
taengdaga
Back
Top