Shruthi R
Shruthi R

Reputation: 1923

Enable only current date till next 30 days - bootstrap datepicker

In rails 3.2.13 project, I am using bootstrap datepicker plugin. Right now calender is displaying the current date by disabling the previous dates. Now I need to restrict the calender to select the current date + next 30 days only.

I am using below code to disable the previous date,

function currentDatePicker(id){
var nowTemp = new Date();
var now = new Date(nowTemp.getFullYear(), nowTemp.getMonth(), nowTemp.getDate(), 0, 0, 0, 0);

 $('#' + id).datepicker({
    format: "yyyy-mm-dd",
    onRender: function(date) {
        return date.valueOf() < now.valueOf() ? 'disabled' : '';
    }
 });
}

Upvotes: 3

Views: 16553

Answers (4)

Khaled Rahman
Khaled Rahman

Reputation: 141

In Bootstrap datepicker Set Current date Use: startDate: new Date() it desible previous date.

Set next 30 days use: endDate: '+30d' It shows only 30 days from the current date

here the example code:

$('#datepicker').datepicker({ 
  startDate: new Date(), // controll start date like startDate: '-2m' m: means Month
  endDate: '+30d'

});

Upvotes: 5

Madhan Mohan
Madhan Mohan

Reputation: 1

Try this its working demo: http://jsfiddle.net/1wsf97pt/

$(function () {
    $("#Id").datepicker({

        minDate: new Date,
        onSelect: function (dateText, inst) {
            try {
                $.datepicker.parseDate('mm/dd/yy', dateText);
            } catch (e) {

                alert(e);

            };
        }
    });
});

Upvotes: -1

Mr.G
Mr.G

Reputation: 3559

Try this:

 $('#datepicker').datepicker({
    startDate: 0,
    endDate: '+30d'
});

Upvotes: 1

Try maxDate

$('#' + id).datepicker({ maxDate: "+30d" });

or

$('#' + id).datepicker( "option", "maxDate", "+30d" );


Updated after OP's comment

fiddle Demo

$('#' + id).datepicker({
    maxDate: "+30d",
    minDate:0
});

minDate

Upvotes: 4

Related Questions