Disable date in Datepicker from SQL Server table

Asked

Viewed 1,185 times

2

I have a system in Classic ASP, in which I have a scheduling calendar. I need to disable the dates that are in the table of holidays and compensations (id_holiday and date). I don’t know how to select an array of unavailable dates.

To do something similar to the variable line unavailableDates.

var days = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"];
var unavailableDates = ["2012/03/26","2012/03/27","2012/04/05"]; // yyyy/MM/dd**
var unavailableDays = ["Saturday","Sunday"];

function unavailable(date) {
ymd = date.getFullYear() + "/" + ("0"+(date.getMonth()+1)).slice(-2) + "/" + ("0"+date.getDate()).slice(-2);
day = new Date(ymd).getDay();
if ($.inArray(ymd, unavailableDates) < 0 && $.inArray(days[day], unavailableDays) < 0) {
    return [true, "enabled", "Book Now"];
} else {
    return [false,"disabled","Booked Out"];
}
}

$('#iDate').datepicker({ beforeShowDay: unavailable });
  • Are you using the datepicker jQuery UI?

  • I don’t know another datepicker with the option "beforeShowDay"

  • mmooser, posted the answer below to try to help you with the script, but I believe your real doubt is how popular the array unavailableDates with the holidays, in this case I will ask you to put the structure of the table of holidays.

  • Toby, my question is regarding the popular yes array. Putting the dates manually I can, but as it is not only the holidays, it is complicated I have to change the file every time. The table tb_holidays has the following structure: id_feriado - int (self-numbered) data - datetime

  • "are not only the holidays, "what else then?

  • They are specific dates such as end of year recess. In addition to the holidays.

Show 1 more comment

1 answer

1

mmooser, follow a slightly cleaner example:

var diasSemana = [ "Domingo", "Segunda", "Terca", "Quarta", "Quinta", "Sexta", "Sabado" ];
var diasFinalSemana = [ "Domingo", "Sabado" ];

var datasIndisponiveis = [
    new Date(2015, 07, 26),
    new Date(2015, 07, 27),
    new Date(2015, 08, 05)
].map(function (data) {
    return data.getTime();
});

$('[date-datepicker]').datepicker({
    beforeShowDay: function(data){        
        var diaSemana = diasSemana[data.getDay()];
        var isDataIndisponivel = datasIndisponiveis.indexOf(data.getTime()) != -1;
        var isDataFinalSemana = diasFinalSemana.indexOf(diaSemana) != -1;
        return [!isDataIndisponivel && !isDataFinalSemana];
    }
});
.ui-widget {
    font-size: 70% !important;
}
<link href="https://code.jquery.com/ui/1.11.4/themes/flick/jquery-ui.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.11.3/jquery-ui.js"></script>
<input type="text" date-datepicker="" />

  • Does anyone know how to fill the array automatically?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.