0

I am trying to call a function testAvailability from within unavailableDays. I need to format the date as a string in selDate using dd-MM-yyyy format. I think I am either not calling the function properly or there is a problem with my date formatting or both.

Any thoughts?

function unavailableDays(days) {
    var i = Date.now();
    var d;
    var checkit 
    var unavailableDates = [];
    var j = i + days * 24 * 60 * 60 * 1000;
    while(i < j) {
        d = new Date(i);

        //add an if then statement here, if true push the date else no
        checkit = testAvailability("'" + d.getDate() + '-' + (d.getMonth() + 1) + '-' + (d.getYear() + 1900) + "'");
        if(checkit ===  true) {
            unavailableDates.push(d.getDate() + '-' + (d.getMonth() + 1) + '-' + (d.getYear() + 1900));
            i += 24 * 60 * 60 * 1000;
        } else {
            i += 24 * 60 * 60 * 1000;
        }
    }
    return unavailableDates;
}

var numOfDays = 60;
var populated = unavailableDays(numOfDays);

...


function testAvailability(selDate) {

    // Find the selected service duration (it is going to 
    // be send within the "postData" object).
    var selServiceDuration = 15; // Default value of duration (in minutes).
    $.each(GlobalVariables.availableServices, function(index, service) {
        if (service['id'] == $('#select-service').val()) {
            selServiceDuration = service['duration']; 
        }
    });

    // If the manage mode is true then the appointment's start 
    // date should return as available too.
    var appointmentId = (FrontendBook.manageMode) 
            ? GlobalVariables.appointmentData['id'] : undefined;

    var postData = {
        'service_id': $('#select-service').val(),
        'provider_id': $('#select-provider').val(),
        'selected_date': selDate,
        'service_duration': selServiceDuration,
        'manage_mode': FrontendBook.manageMode,
        'appointment_id': appointmentId
    };

    // Make ajax post request and get the available hours.
    var ajaxurl = GlobalVariables.baseUrl + 'appointments/ajax_get_available_hours';
    jQuery.post(ajaxurl, postData, function(response) {
        ///////////////////////////////////////////////////////////////
        console.log('Get Available Hours JSON Response:', response);
        ///////////////////////////////////////////////////////////////

        if (!GeneralFunctions.handleAjaxExceptions(response));

        // The response tells me if the date is booked (true) or not. 
        if (response.length > 0) {
           return false;
        } else {
           return true;
        }
    }, 'json');
}
cravaus
  • 13
  • 3

2 Answers2

1

I am assuming that testAvailability() check for the date string in the unavailableDates array. You are testing with quotes, but you are recording the string without quotes, so there's no match.

var datestr;
// ..

while(i < j) {
    d = new Date(i);
    datestr = "" + d.getDate() + '-' + (d.getMonth() + 1) + '-' + (d.getYear() + 1900);

    if (testAvailability(datestr) === true) unavailableDates.push(datestr);
    i += 24 * 60 * 60 * 1000;
}

EDIT after OP posted relevant code:

Here's your problem: the testAvailability function doesn't return a boolean (or anything for that matter), it's an asynchronous function. Your code can't possibly work.

sebnukem
  • 7,217
  • 5
  • 35
  • 45
0

Move all that date formatting to single variable:

while (i < j) {
    d = new Date(i);
    var newDate = d.getDate() + '-' + (d.getMonth() + 1) + '-' + (d.getYear() + 1900);

Also I don't think you need ' around parameter of function:

testAvailability(newDate);
Justinas
  • 34,232
  • 3
  • 56
  • 78
  • op is formatting date created inside `while` loop. So he can't format it outside the loop. Also he's sending formatted date as string to `testAvailability` so he needs those quotes. – Shoaib Shakeel Apr 13 '15 at 04:07