Andrew_DX February 2016

How to select a range of two dates with Datepicker

I have two datepicker boxes:

$('.datepicker1').datepicker();
$('.datepicker2').datepicker();

Assume that the datepicker1 = 10/02/2016 and the datepicker2 = 15/02/2016.

How can I make a conditional statement that looks something like: "if the two dates are in the range between 01/01/2016 and 01/03/2016, do something..."? How can I set this range?

I need to do different calculations based on this range with the two dates picked up.

Thanks in Advance!

Answers


Shriya R February 2016

$(document).ready(function () {

    $("#dt1").datepicker({
        dateFormat: "dd-M-yy",
        minDate: 0,
        onSelect: function (date) {
            var date2 = $('#dt1').datepicker('getDate');
            date2.setDate(date2.getDate() + 1);
            $('#dt2').datepicker('setDate', date2);
            //sets minDate to dt1 date + 1
            $('#dt2').datepicker('option', 'minDate', date2);
        }
    });
    $('#dt2').datepicker({
        dateFormat: "dd-M-yy",
        onClose: function () {
            var dt1 = $('#dt1').datepicker('getDate');
            var dt2 = $('#dt2').datepicker('getDate');
            //check to prevent a user from entering a date below date of dt1
            if (dt2 <= dt1) {
                var minDate = $('#dt2').datepicker('option', 'minDate');
                $('#dt2').datepicker('setDate', minDate);
            }
        }
    });
});

Post Status

Asked in February 2016
Viewed 1,562 times
Voted 6
Answered 1 times

Search




Leave an answer