RedDragon
RedDragon

Reputation: 2138

Restrict selection on datepicker with viewMode set to years

I am using the solution provided on How display only years in input Bootstrap Datepicker? to show only years on datepicker.

Live DEMO

My datepicker has the following configuration:

$("#datepicker").datepicker( {
    format: " yyyy", // Notice the Extra space at the beginning
    viewMode: "years", 
    minViewMode: "years"
});

I've tried to set endDate: '0d' and it didn't work.

How can I set an endDate which will prevent me from selecting a future year using that solution?

Upvotes: 2

Views: 630

Answers (2)

Andrew Tomlinson
Andrew Tomlinson

Reputation: 181

Worked for me by passing the endDate attribute as the current date object retrieved by the Date() default constructor.

$("#datepicker").datepicker( {
    format: " yyyy", // Notice the Extra space at the beginning
    viewMode: "years", 
    minViewMode: "years",
    endDate: new Date()
});

By playing around with the JavaScript Date object, you should be able to set the max to be a date within any year you want.

Upvotes: 1

brandelizer
brandelizer

Reputation: 352

Try this:

$( "#datepicker" ).datepicker( { minDate: -0, maxDate: new Date(2013, 1,18) });

Upvotes: 0

Related Questions