Reputation: 1923
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
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:
<input id="dp1" type="text" data-date-format="yyyy-mm-dd">
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
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