3

I have javascript function

$(document).ready(function () {

var d = new Date();
var monthNames = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"];
today = monthNames[d.getMonth()] + ' ' + d.getDate() + ' ' + d.getFullYear();

$('#to').attr('disabled', 'disabled');
$('#from').datepicker({
    defaultDate: "+1d",
    beforeShowDay: DisableSpecificDates,
    minDate: "+1d",
    maxDate: "+3M",
    dateFormat: 'dd.mm.yy',
    showOtherMonths: true,
    changeMonth: true,
    selectOtherMonths: true,
    required: true,
    altField: '#checkin',
    numberOfMonths: 1,
});

$('#from').change(function () {
    var from = $('#from').datepicker('getDate');
    var date_diff = Math.ceil((from.getTime() - Date.parse(today)) / 86400000);
    var maxDate_d = date_diff+7+'m';
    date_diff = date_diff + 1;
    $('#to').val('').removeAttr('disabled').removeClass('hasDatepicker').datepicker({
        dateFormat: 'dd.mm.yy',
        minDate: date_diff,
        maxDate: maxDate_d,
        altField: '#checkout',
    });
});

$('#to').keyup(function () {
    $(this).val('');
    alert('Please select date from Calendar');
});
$('#from').keyup(function () {
    $('#from,#to').val('');
    $('#to').attr('disabled', 'disabled');
    alert('Please select date from Calendar');
});

This allows me to create JavaScript date select in form (check in and check out dates)

Function works perfect but here need one additional function to be integrated.

I want disable specific dates from - to I found this JavaScript and want to integrate in function above.

     var dakavebuli = 1000*60*60*24;
boloshesvla = new Date('2015-12-24');
bologasvla =  new Date("2015-12-25");
var shualeduritarigebi = (bologasvla.getTime()-          boloshesvla.getTime())/dakavebuli;


for(var i=0;i<=shualeduritarigebi; i++)
{
   var checking = boloshesvla.getTime()+dakavebuli*i;
   var checkout = new Date(checking);

   alert (checkout.getFullYear()+"-"+(checkout.getMonth()+1)+"-"+checkout.getDate());
}
2
  • It is not clear what the new javascript is supposed to do and what is the issue you found. Commented Dec 25, 2015 at 10:25
  • I have hotel reservation form and want to disable that dates wich already is taken Commented Dec 25, 2015 at 10:32

2 Answers 2

1

The function you are using is not returning anything to the datepicker and hence will not block any such dates, you can use this function:

These are the dates stored in an array to be blocked.

var $myBadDates = new Array("10 October 2010","21 November 2010","12 December 2010","13 January 2011","14 February 2011","15 March 2011");

Function definition:

function DisableSpecificDates(mydate){
var $return=true;
var $returnclass ="available";
$checkdate = $.datepicker.formatDate('dd MM yy', mydate);
for(var i = 0; i < $myBadDates.length; i++)
    {    
       if($myBadDates[i] == $checkdate)
          {
        $return = false;
        $returnclass= "unavailable";
        }
    }
return [$return,$returnclass];
}
Sign up to request clarification or add additional context in comments.

4 Comments

I have two variables (from database) check in date ($dakavebulidan) and check out ($dakavebulimde) this looks like tgis : 2015-12-24 and 2015-12-25 first I want find dates between two dates and after disable oll
Yes you can loop between these two dates to get all dates, refer stackoverflow.com/a/18109599/1496715
can you show finished function from that? I am new in javascript thanks!
I try but some errors here is live site url holidaylux.ge/en/rooms/items/item/6-holiday-lux-batumi2
0

here is SOLUTION for this question

$(document).ready(function () {

var d = new Date();

var unavailableDates = [<?=$disabledays?>];

function unavailable(date) {
dmy = date.getDate() + "-" + (date.getMonth() + 1) + "-" + date.getFullYear();
if ($.inArray(dmy, unavailableDates) == -1) {
    return [true, ""];
} else {
    return [false, "", "Unavailable"];
}
}


var monthNames = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"];
today = monthNames[d.getMonth()] + ' ' + d.getDate() + ' ' + d.getFullYear();

$('#to').attr('disabled', 'disabled');
$('#from').datepicker({
    defaultDate: "+1d",
    beforeShowDay: unavailable,
    minDate: "+1d",
    maxDate: "+1M",
    dateFormat: 'dd.mm.yy',
    showOtherMonths: true,
    changeMonth: true,
    selectOtherMonths: true,
    required: true,
    altField: '#checkin',
    numberOfMonths: 1,
});

$('#from').change(function () {
    var from = $('#from').datepicker('getDate');
    var date_diff = Math.ceil((from.getTime() - Date.parse(today)) / 86400000);
    var maxDate_d = date_diff+7+'m';
    date_diff = date_diff + 1;
    $('#to').val('').removeAttr('disabled').removeClass('hasDatepicker').datepicker({
        dateFormat: 'dd.mm.yy',
        minDate: date_diff,
        maxDate: maxDate_d,
        beforeShowDay: unavailable,
        altField: '#checkout',
    });
});

$('#to').keyup(function () {
    $(this).val('');
    alert('Please select date from Calendar');
});
$('#from').keyup(function () {
    $('#from,#to').val('');
    $('#to').attr('disabled', 'disabled');
    alert('Please select date from Calendar');
});

});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.