Using the value of a datapicker in another

2

I'm trying to use this DataPicker to pick the day he selects, add another 7 days and from these days, return only on Saturdays. Follow the code.     Updated code, now it only needs to add up to 7 days.

$("#data-inicio").datepicker({
    beforeShowDay: function(date){ return [date.getDay() == 1, ""] },
    minDate: current,
    onSelect: function (selectedDate) {
        $("#data-retorno").datepicker("option", "minDate", selectedDate);
        $("#data-retorno").datepicker("refresh");
    }
});
   $("#data-retorno").datepicker({
    onClose: function (selectedDate) {
        $("#data-inicio").datepicker("option", "maxDate", selectedDate);
    },
    beforeShowDay: function(date){ return [date.getDay() == 5, ""] },    
    });

});

    
asked by anonymous 28.04.2015 / 04:04

1 answer

2

With some information passed by the author through the chat, we come to a code with the result expected by it:

Javascript:

$(document).ready(function () {
    //Data atual
    var current = new Date();
    //Somará 30 dias
    var aux = new Date(current.getFullYear(), current.getMonth(), current.getDate() + 30);
    if (aux.getDay() != 1) {
        var nextMonth = new Date(aux.getFullYear(), aux.getMonth() + 1, 1);
        var days = Math.floor((nextMonth - aux) / (1000 * 60 * 60 * 24))

        if (days < 7) current = new Date(aux.getFullYear(), aux.getMonth(), aux.getDate() + days);
        else current = aux;
    } else {
        current = aux;
    }

    $("#data-inicio").datepicker({
        minDate: current,
        dateFormat: 'dd/mm/yy', //Formato da data
        beforeShowDay: function (date) {
            return [date.getDay() == 1, ""]
        },
        onSelect: function (selectedDate) {
            //Limpamos a segunda data, para evitar problemas do usuário ficar trocando a data do primeiro datepicker e acabar burlando as regras definidas.
            $.datepicker._clearDate($("#data-retorno"));
            //Aqui está a "jogada" para somar os 7 dias para o próximo datepicker.
            var data = $.datepicker.parseDate('dd/mm/yy', selectedDate);
            data.setDate(data.getDate('dd/mm/yy') + 7); //Soma 7 dias a data atual
            $("#data-retorno").datepicker("option", "minDate", data); //Aplica a data
        }
    });
    //Segundo datepicker
    $("#data-retorno").datepicker({
        dateFormat: 'dd/mm/yy', //Formatação
        onClose: function (selectedDate) {
            $("#data-inicio").datepicker("option", "maxDate", selectedDate);
        },
        beforeShowDay: function (date) {
            return [date.getDay() == 5, ""]
        }
    });

});

HTML for example:

<input type="text" id="data-inicio" name="data-inicio" />
<input type="text" id="data-retorno" name="data-retorno" />

Running: link

    
28.04.2015 / 16:47