santhosh santhosh - 2 months ago 14
Ajax Question

I want datepicker in that END date as current date and START date as 7 days previous to END date in Jquery r Bootstrap

$(document).ready(function () {
$("#end").datepicker({
dateFormat: "dd-M-yy",
minDate: 0,
onSelect: function () {
var start = $('#start');
var startDate = $(this).datepicker('getDate');
startDate.setDate(startDate.getDate() - 7);
start.datepicker('option', 'minDate', startDate);
start.datepicker('option', 'maxDate',$(this).datepicker('getDate'));
}
});
$('#start').datepicker({
dateFormat: "dd-M-yy"
});
});


Above is my code and I want datepicker in that END date as current date and START date as 7 days previous to END date in Jquery r Bootstrap.

Answer

HTML

<input type="text" id="start">
<input type="text" id="end">

Jquery

$(document).ready(function () {
$("#end").datepicker({
    dateFormat: "dd-M-yy",
    minDate: 0,
    onSelect: function () {
        var start = $('#start');
        var startDate = $(this).datepicker('getDate');
        startDate.setDate(startDate.getDate() - 7);
        start.datepicker('option', 'minDate', startDate);
        start.datepicker('option', 'maxDate', $(this).datepicker('getDate'));
    }
});
$('#start').datepicker({
    dateFormat: "dd-M-yy"
});
});

Demo

Hope this helps.