Jay
Jay

Reputation: 723

Setup Min Date in Bootstrap Datepicker

In My case i want to change mindate dynamically when changing of one input

$('#text1').on('changeDate', function () {
                var sdate = moment.utc((<any>$('#text1')).datepicker('getFormattedDate')).subtract(1, "days");
                (<any>$("#LossDate")).datepicker('minDate', sdate.format("MM-DD-YYYY"));
            });

i am also try with

$('#text1').on('changeDate', function () {
                    var sdate = moment.utc((<any>$('#text1')).datepicker('getFormattedDate')).subtract(1, "days");
                    (<any>$("#LossDate")).datepicker('setStartDate', sdate.format("MM-DD-YYYY"));
                });

and

 $('#text1').on('changeDate', function () {
                        var sdate = moment.utc((<any>$('#text1')).datepicker('getFormattedDate')).subtract(1, "days");
                        (<any>$("#LossDate")).datepicker('startDate', sdate.format("MM-DD-YYYY"));
                    });

and

 $('#text1').on('changeDate', function () {
                            var sdate = moment.utc((<any>$('#text1')).datepicker('getFormattedDate')).subtract(1, "days");
                            (<any>$("#LossDate")).datepicker('SetstartDate', sdate.format("MM-DD-YYYY"));
                        });

but nothing is working in my case

Upvotes: 1

Views: 675

Answers (2)

Aparajit P Utpat
Aparajit P Utpat

Reputation: 385

Use this

$("#text1").datepicker({
 autoclose: true
}).on('changeDate', function (selected) {
    var minDate = new Date('2017-12-01');
    $('#text1').datepicker('setStartDate', minDate);
});

Upvotes: 0

Darshit Hedpara
Darshit Hedpara

Reputation: 670

Here is the working javascript:

$(document).ready(function(){
  
    $("#startdate").datepicker({
        todayBtn:  1,
        autoclose: true,
    }).on('changeDate', function (selected) {
        var minDate = new Date(selected.date.valueOf());
        $('#enddate').datepicker('setStartDate', minDate);
    });
    
    $("#enddate").datepicker()
        .on('changeDate', function (selected) {
            var minDate = new Date(selected.date.valueOf());
            $('#startdate').datepicker('setEndDate', minDate);
        });

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.4.0/css/bootstrap-datepicker.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.4.0/js/bootstrap-datepicker.min.js"></script>

<div class="row">
    <div class="col-xs-6">
        <div class="form-group">
            <label class="control-label">Start</label>
            <input class="form-control" type="text" placehoder="Start Date" id="startdate"/>
        </div>
    </div>
    <div class="col-xs-6">
        <div class="form-group">
            <label class="control-label">End</label>
            <input class="form-control" type="text" placehoder="End Date" id="enddate"/>
        </div>
    </div>
</div>

Happy Coding

Upvotes: 3

Related Questions