我试着编写一些逻辑,以便在下一次出现时基于当前日期和时间的一组模型中进行数据绑定
样本数据如下。天代表计划的每一天,小时是它开始的小时,启用是自解释的:
var collection = [{
days: [0, 1, 2, 3, 4, 5, 6],
hour: 8,
enabled: true }, {
days: [0, 1, 4, 5, 6],
hour: 14,
enabled: true }, {
days: [0, 1, 2, 3, 4, 6],
hour: 2,
enabled: false }, {
days: [0, 1, 2, 3, 4, 6],
hour: 14,
enabled: true }];
到目前为止,这是我进步的一小部分:https://jsfiddle.net/5L5q01yk/2/ 到目前为止,它检查了今天发生的任何时间表,然后找出最接近当前时间的可能性。无论是否今天,我都需要它成为下一个场合。
感谢任何帮助
答案 0 :(得分:1)
我通过将您的初始输入映射到表示日程表中下一个可用日期的日期数组来解决这个问题,然后将结果简化为最小值很简单。你可以在一步中真正做到这一点,但中间数据结构可能对你有用,所以我离开了它。
var collection = [{
days: [0, 1, 2, 3, 4, 5, 6],
hour: 8,
enabled: true
}, {
days: [0, 1, 4, 5, 6],
hour: 14,
enabled: true
}, {
days: [0, 1, 2, 3, 4, 6],
hour: 2,
enabled: false
}, {
days: [0, 1, 2, 3, 4, 6],
hour: 14,
enabled: true
}];
var nextTime = null;
var now = new Date();
var dayOfMonth = now.getDate();
var month = now.getMonth();
var dayOfWeek = now.getDay();
var year = now.getFullYear();
var hour = now.getHours();
var nextPossibleTimes = collection.map(function(daySchedule) {
daySchedule.days = daySchedule.days.map(function(scheduledDay) {
var dayOffset = scheduledDay - dayOfWeek;
if ((scheduledDay < dayOfWeek) || (scheduledDay === dayOfWeek && daySchedule.hour < hour)) {
//add a week
dayOffset += 7;
}
return new Date(year, month, dayOfMonth + dayOffset, daySchedule.hour);
});
return daySchedule;
});
nextPossibleTimes.forEach(function(current) {
current.days.forEach(function(date) {
if (nextTime === null || nextTime > date)
nextTime = date;
});
});
var dateArr = nextTime.toString().split(" ");
var dateName = dateArr[0] + " " + dateArr[4];
console.log("Next available time: " + dateName);
&#13;
<script src="http://gh-canon.github.io/stack-snippet-console/console.min.js"></script>
&#13;