jQuery datepicker years shown

后端 未结 8 1271
一向
一向 2020-12-13 01:51

With the jQuery datepicker, how does one change the year range that is displayed? On the jQuery UI site it says the default is \"10 years before and after the current year

相关标签:
8条回答
  • 2020-12-13 01:54

    what no one else has put is that you can also set hard-coded date ranges:

    for example:

    yearRange: "1901:2012"
    

    whilst it may be advisable to not do this, it is however, an option that is perfectly valid (and useful if you are legitimately looking for say a specific year in a catalogue - such as "1963:1984" ).

    0 讨论(0)
  • 2020-12-13 01:55
     $("#DateOfBirth").datepicker({
            yearRange: "-100:+0",
            changeMonth: true,
            changeYear: true,
        });
    

    yearRange: '1950:2013', // specifying a hard coded year range or this way

    yearRange: "-100:+0", // last hundred years

    It will help to show drop down for year and month selection.

    0 讨论(0)
  • 2020-12-13 01:56

    Why not show the year or month selection boxes?

    $( ".datefield" ).datepicker({
        changeMonth: true,
        changeYear: true,
        yearRange:'-90:+0'
    });
    
    0 讨论(0)
  • 2020-12-13 01:56

    Perfect for date of birth fields (and what I use) is similar to what Shog9 said, although I'm going to give a more specific DOB example:

    $(".datePickerDOB").datepicker({ 
        yearRange: "-122:-18", //18 years or older up to 122yo (oldest person ever, can be sensibly set to something much smaller in most cases)
        maxDate: "-18Y", //Will only allow the selection of dates more than 18 years ago, useful if you need to restrict this
        minDate: "-122Y"
    });
    

    Hope future googlers find this useful :).

    0 讨论(0)
  • 2020-12-13 02:00

    If you look down the demo page a bit, you'll see a "Restricting Datepicker" section. Use the dropdown to specify the "Year dropdown shows last 20 years" demo , and hit view source:

    $("#restricting").datepicker({ 
        yearRange: "-20:+0", // this is the option you're looking for
        showOn: "both", 
        buttonImage: "templates/images/calendar.gif", 
        buttonImageOnly: true 
    });
    

    You'll want to do the same (obviously changing -20 to -100 or something).

    0 讨论(0)
  • 2020-12-13 02:00

    Adding to what @Shog9 posted, you can also restrict dates individually in the beforeShowDay: callback function.

    You supply a function that takes a date and returns a boolean array:

    "$(".selector").datepicker({ beforeShowDay: nationalDays}) 
    natDays = [[1, 26, 'au'], [2, 6, 'nz'], [3, 17, 'ie'], [4, 27, 'za'], 
    [5, 25, 'ar'], [6, 6, 'se'], [7, 4, 'us'], [8, 17, 'id'], [9, 7, 
    'br'], [10, 1, 'cn'], [11, 22, 'lb'], [12, 12, 'ke']]; 
    function nationalDays(date) { 
        for (i = 0; i < natDays.length; i++) { 
          if (date.getMonth() == natDays[i][0] - 1 && date.getDate() == 
    natDays[i][1]) { 
            return [false, natDays[i][2] + '_day']; 
          } 
        } 
      return [true, '']; 
    } 
    
    0 讨论(0)
提交回复
热议问题