How to limit an interval of 7 jQuery days?

1

I have the following code, my question is: How can I limit so that the date is only 7 days backwards or 7 days ahead?

$(document).ready(function() {

  $('#fecha1').datepicker({
     startDate: '2015-12-01',
     format: 'YYYY-MM-DD'
   });


  $('#fecha1').datepicker().on('changeDate', function(e) {
    fInicial = getFormattedDate('fecha1');
    $(this).datepicker('hide');
  });

  $('#fecha2').datepicker().on('changeDate', function(e) {
    fFinal = getFormattedDate('fecha2');
    $(this).datepicker('hide');


    var jqxhr = $.post({
        url: 'assets/lib/infos.php',
        data: {
               inicio: fInicial,
               fin: fFinal
             },
        dataType: "JSON"
      })
      .done(function(json) {

        //codigo


      })
  });

});

as well as in this example:

    
asked by Houdini 12.04.2018 в 20:12
source

2 answers

0

According to the documentation in jqueryui :

maxDate

  

The maximum selectable date. When set to null, there is no maximum.   Multiple types supported:

     
  • Date : A date object containing the maximum date.
  •   
  • Number : A number of days from today. For example% co_of% represents two days from today and% co_of% represents yesterday.
  •   
  • String : A string in the format defined by the dateFormat option, or a relative date. Relative dates must contain value and period pairs;   valid periods are 2 for years, -1 for months, "y" for weeks, and    "m" for days. For example, "w" represents one month and seven   days from today.
  •   

Or in Spanish:

  

The maximum date selectable. When it is null, there is no maximum.   Multiple supported types:

     
  • Date : A date object containing the maximum date value.
  •   
  • Number : A number of days from today. For example "d" represents two days from today and "+1m +7d" represents yesterday.
  •   
  • String : A string in the format defined by the dateFormat option, or a relative date. Relative dates must contain value and period pairs;   the valid periods are 2 for years, -1 for months, "y" for weeks, and    "m" for days. For example, "w" represents one month and seven   days from today.
  •   

In other words you have 2 simple options:

  • Using the relative string format:

    $( ".selector" ).datepicker({
      minDate: "-7d",
      maxDate: "+7d"
    });
    
  • Using the number of days:

    $( ".selector" ).datepicker({
      minDate: -7,
      maxDate: 7
    });
    
answered by 12.04.2018 в 20:39
0

If you are using link , you can play with the constructor options, passing an initial and final date depending on the range you want to create. If you pass a string string with the format + d ( Example: + 2d) it will add 2 days to the initial date, if you have not defined it is the current date and if you have the form - d ( Example: a-3d), it will subtract you from so many days you have indicated, in this case 3.

SO THAT IT IS the last 7 days until today

$('#fecha1').datepicker({
 startDate: '-7d',
 endDate: '0d'
 format: 'YYYY-MM-DD'
});

TO BE THE next 7 days including today

$('#fecha1').datepicker({
 startDate: new Date(),
 endDate: '+7d'
 format: 'YYYY-MM-DD'
});
    
answered by 12.04.2018 в 20:40