Shruthi R
Shruthi R

Reputation: 1923

jQuery - Not able to disable past dates using twitter bootstrap's datepicker

In rails 3.2.9, i am using twitter bootstrap gem & trying to use its datepicker. In this datepicker tool i need to disable the past dates, i need to enable the current date & future dates. But now i am not able disable past dates.

I have tried in different ways,

Eg 1 :

function scriptDatePicker(id){
var today = new Date();
var dd = today.getDate();
var mm = ("0" + (today.getMonth() + 1)).slice(-2) //January is 0!
var yyyy = today.getFullYear();
$('#' + id).datepicker({
    format: "yyyy-mm-dd",
    startDate: yyyy + '-' +  mm + '-' + dd
});
}

Eg 2 :

$('#id').datepicker({startDate: '01/01/2013'});

Eg 3 :

$("#datepicker").datepicker({ minDate: 0 });

Eg 4 :

$('.datepicker').datepicker('setStartDate', '2013-01-01');

And also i have tried some other examples but it is not working. Please help me to solve this issue.

Upvotes: 4

Views: 6731

Answers (2)

gco
gco

Reputation: 1690

For everybody who wants to show only the current year and disable all dates from the past years and the future years as well. This is it:

Markup

<input id="dp1" type="text" data-date-format="yyyy-mm-dd">

Javascript

var nowTemp = new Date();
var now = new Date(nowTemp.getFullYear());

$('#dp1').datepicker({
  onRender: function(date) {
    return date.getFullYear() < now.valueOf() ? 'disabled' : (date.getFullYear() > now.valueOf() ? 'disabled' : '');
  }
});

Upvotes: 0

Miljan Puzović
Miljan Puzović

Reputation: 5820

HTML markup:

<input id="dpd1" type="text" data-date-format="yyyy-mm-dd">

jQuery:

var nowTemp = new Date();
var now = new Date(nowTemp.getFullYear(), nowTemp.getMonth(), nowTemp.getDate(), 0, 0, 0, 0);

$('#dpd1').datepicker({
  onRender: function(date) {
    return date.valueOf() < now.valueOf() ? 'disabled' : '';
  }
});

Live example: http://jsfiddle.net/Jdqvk/2/

Upvotes: 6

Related Questions