JS使用日期格式调用函数内的函数

时间:2015-04-13 04:00:43

标签: javascript function date

我正在尝试从testAvailability内调用函数unavailableDays。我需要使用selDate格式将日期格式化为dd-MM-yyyy中的字符串。我想我要么没有正确调用该函数,要么我的日期格式有问题,或者两者兼而有之。

有什么想法?

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');
}

2 个答案:

答案 0 :(得分:1)

我假设testAvailability()检查unavailableDates数组中的日期字符串。您正在测试引号,但是您正在录制没有引号的字符串,因此没有匹配。

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;
}
OP发布相关代码后编辑:

这是你的问题:testAvailability函数没有返回布尔值(或任何事情),它是一个异步函数。您的代码可能无法正常工作。

答案 1 :(得分:0)

将所有日期格式移动到单个变量:

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

另外,我认为你不需要'函数参数:

testAvailability(newDate);