Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery UI Datepicker - onSelect get the selected date +3 days

I try to create a date range by using the jQuery UI datepicker, using two text fields. The first text field "start_date" will set the start date and the second text field "end_date" will set the end date.

The code I have till now is this:

$('#start_date').live(
    'focus',
    function()
    {
        $('#end_date').datepicker(
            {
                dateFormat: 'dd MM yy',
                minDate: new Date(),
                maxDate: new Date(2012, 9, 15),
                stepMonths: 2,
                numberOfMonths: 2
            }
        );

        $(this).datepicker(
            {
                dateFormat: 'dd MM yy',
                minDate: new Date(),
                maxDate: new Date(2012, 9, 15),
                stepMonths: 2,
                numberOfMonths: 2,
                onSelect: function(dateText, inst)
                {
                    var instance = $( this ).data("datepicker");

                    var date = $.datepicker.parseDate(instance.settings.dateFormat, dateText, instance.settings);

                    $('#end_date').datepicker('option', 'minDate', dateText);
                }
            }
        );
    }
);

NOTE : I use the live and the focus event because all my page content is loaded with AJAX call. Maybe that is correct or wrong, but this is not what I like to ask here ;)

The above code is correct for me and work fine, but what I like to do, is to set the value of the 'end_date' element to selected one +3 days.

Until now, when I choose a date at the "start_date" element the "end_date" element change to the same date as the "start_date", and this is what I like to change. The "end_date" to be +3 days from the "start_date" element adter the selection.

How can I do that ?

like image 633
KodeFor.Me Avatar asked Dec 02 '22 22:12

KodeFor.Me


1 Answers

To set end_date for +1 day

onSelect: function(dateText, inst) {
    $('#start_date').datepicker('option','minDate', new Date(inst.selectedYear, inst.selectedMonth, inst.selectedDay));
    var toDate = new Date(inst.selectedYear, inst.selectedMonth, inst.selectedDay);//Date one month after selected date
    var oneDay = new Date(toDate.getTime()+86400000);
    document.getElementById('end_date').value =$.datepicker.formatDate('dd/mm/yy', oneDay);
}
like image 168
Daya Avatar answered Dec 19 '22 15:12

Daya