Bootstrap Datepicker Startdate from Attribute

I know that you can set startdate when initializing your datepikers in the configuration settings. I have not found a way to add an attribute inside html input to do this.

The way I got around this was like that.

$('.date-picker').each(function () {
    if($(this).attr("startdate") !== undefined) {
        $(this).datepicker({ format: "dd/mm/yyyy", weekStart: 0, autoclose: true, startDate: $(this).attr("startdate") });
    }
    else {
        $(this).datepicker({ format: "dd/mm/yyyy", weekStart: 0, autoclose: true });
    }
});

I write this in Razor with the following

@Html.TextBoxFor(model => model.SomeDate, new { @class = "date-picker", @data_date = "12-02-2012", @startdate = "12-04-2012" })

Can anyone suggest the right way?

This works for me, which is cool now, it adds a disabled css class to anything until date 12-04-2012.

+3
source share
1 answer

You need to use .data (), not .attr ()

<input type="text" class="date_picker" data-start-date="2012-01-01">

and then

$('.date-picker').each(function () {
  var config = { format: "dd/mm/yyyy", weekStart: 0, autoclose: true  };
  if ($(this).data("startDate") != undefined) {
    config.startDate = $(this).data("startDate");
  }
  $(this).datepicker(config);  
});
+2
source

All Articles