Skip to content Skip to sidebar Skip to footer

Datepicker Limit Current Year

I have a little problem with datepicker, I'd want that a user with arrows can only select a date between the first day of the first month and the last day of the month, but it woul

Solution 1:

Set minDate and maxDate like

$(function() {
    var year = (new Date).getFullYear();
    $( "#datepicker" ).datepicker({
        minDate: new Date(year, 0, 1),
        maxDate: new Date(year, 11, 31)
    });
});

Link


Solution 2:

Just use yearRange like this:

datepick({yearRange: new Date().getFullYear() + ':' + new Date().getFullYear()});

Solution 3:

Why not mention the yearRange to be currentyear : currentyear?

You can do this: after

$(document).ready(function(){
var curyear = $("#year").text( (new Date).getFullYear() );

Inside your datepicker, yearRange: "+curyear+":"+curyear+"


Solution 4:

This will work for you:

jsFiddle

<head>
  <meta charset="utf-8" />
  <link  href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
  <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
  <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
  <link  href="/resources/demos/style.css" />
  <script>

    $(document).ready(function() {

        var date = new Date(), y = date.getFullYear(), m = date.getMonth();
        var fSDate = new Date(y, m, 1);
        var fEDate = new Date(y, m + 1, 0);

        $.datepicker.setDefaults({
            changeMonth: false,
            changeYear: false,
            showOtherMonths: false,
            yearRange: '-0:+0',
            dateFormat: 'yy-mm-dd',
            defaultDate: +0,
            numberOfMonths: 1,
            minDate: fSDate, 
            maxDate: fEDate,
            showAnim: 'fadeIn',
            showButtonPanel: false
        });

        $('#date_start').datepicker();

    }); //END document.ready()
  </script>
</head>
<body>

    <input type="text" id="date_start">

</body>
</html>

Post a Comment for "Datepicker Limit Current Year"