gpt4 book ai didi

javascript - JS 在函数内调用函数并设置日期格式

转载 作者:行者123 更新时间:2023-12-03 10:21:42 27 4
gpt4 key购买 nike

我正在尝试调用一个函数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');
}

最佳答案

我假设 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 函数不返回 bool 值(或任何与此相关的内容),它是一个异步函数。您的代码不可能工作。

关于javascript - JS 在函数内调用函数并设置日期格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29597653/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com