我正在使用全日历预订系统,人们可以选择要预订的日期和时间。
在没有开始和结束时间的情况下,我可以使用日历。一旦给节点一个开始和结束时间,我就会得到一个无限循环。下面,我将显示工作代码的javascript和日历的屏幕截图,其次是带有开始和结束时间的代码以及无限循环问题的屏幕截图。
目标是获取第一个屏幕快照,但要包含开始时间和结束时间。
工作代码
document.addEventListener('DOMContentLoaded', function () {
var calendarEl = document.getElementById('calendar');
var getDates = function (startDate, endDate) {
var dates = [],
currentDate = startDate,
addDays = function (days) {
var date = new Date(this.valueOf());
date.setDate(date.getDate() + days);
return date;
};
while (currentDate <= endDate) {
dates.push(currentDate);
currentDate = addDays.call(currentDate, 1);
}
return dates;
};
var nonAvailableList = @Html.Raw(Json.Encode(Model.NonAvailableDates));
var newArr = [];
var dates = getDates(new Date('2020-07-03'), new Date('2029-12-31'));
var id = 0;
var counter = 0;
dates.forEach(function (date)
{
var day = ("0" + date.getDate()).slice(-2);
var month = ('0' + (date.getMonth() + 1)).slice(-2);
var year = date.getFullYear();
for (var i = 1; i <= 2; i++)
{
if (nonAvailableList.length != 0) {
var firstDate = nonAvailableList[counter].Date;
var firstSlot = nonAvailableList[counter].TimeSlot;
}
var newObj = { id: "", start: "", end: "", startTime: "", endTime: "", title: "", color: ""};
newObj.id = id++;
newObj.start = year + "-" + month + "-" + day;
newObj.end = year + "-" + month + "-" + day;
newObj.title = i.toString();
var currentDate = day + "-" + month + "-" + year;
if (nonAvailableList.length != 0) {
if (firstDate == currentDate && firstSlot == newObj.title) {
if (counter < nonAvailableList.length-1) {
counter++;
}
newObj.color = "#FF0000";
}
}
newArr.push(newObj);
};
});
不起作用
document.addEventListener('DOMContentLoaded', function () {
var calendarEl = document.getElementById('calendar');
var getDates = function (startDate, endDate) {
var dates = [],
currentDate = startDate,
addDays = function (days) {
var date = new Date(this.valueOf());
date.setDate(date.getDate() + days);
return date;
};
while (currentDate <= endDate) {
dates.push(currentDate);
currentDate = addDays.call(currentDate, 1);
}
return dates;
};
var nonAvailableList = @Html.Raw(Json.Encode(Model.NonAvailableDates));
var newArr = [];
var dates = getDates(new Date('2020-07-03'), new Date('2029-12-31'));
var id = 0;
var counter = 0;
dates.forEach(function (date)
{
var day = ("0" + date.getDate()).slice(-2);
var month = ('0' + (date.getMonth() + 1)).slice(-2);
var year = date.getFullYear();
for (var i = 1; i <= 2; i++)
{
if (nonAvailableList.length != 0) {
var firstDate = nonAvailableList[counter].Date;
var firstSlot = nonAvailableList[counter].TimeSlot;
}
var newObj = { id: "", start: "", end: "", startTime: "", endTime: "", title: "", color: ""};
newObj.id = id++;
newObj.start = year + "-" + month + "-" + day;
newObj.end = year + "-" + month + "-" + day;
newObj.title = i.toString();
**if (i == 1) {
newObj.startTime = "11:00";
newObj.endTime = "13:30";
} else if (i == 2) {
newObj.startTime = "14:00";
newObj.endTime = "16:30";
}**
var currentDate = day + "-" + month + "-" + year;
if (nonAvailableList.length != 0) {
if (firstDate == currentDate && firstSlot == newObj.title) {
if (counter < nonAvailableList.length-1) {
counter++;
}
newObj.color = "#FF0000";
}
}
newArr.push(newObj);
};
});
唯一的1和2之间的代码是
newObj.startTime = "11:00";
newObj.endTime = "13:30";
} else if (i == 2) {
newObj.startTime = "14:00";
newObj.endTime = "16:30";
}
所以问题必须在某个地方,但我似乎找不到。