You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

预订系统开发:如何筛选自次日当前时间起24小时内的可用时段

实现预订系统的24小时时段筛选逻辑

我来帮你搞定这个新的时段筛选功能,核心思路是先明确要筛选的时间范围,再对数据库数据进行过滤,只保留符合条件的可用时段。

步骤1:计算筛选的时间范围

首先我们需要基于当前时间,算出两个关键时间点:

  • 起始时间:次日的当前时间(比如当前是2019-07-08 11:30,起始时间就是2019-07-09 11:30)
  • 结束时间:再次日的当前时间(也就是起始时间加24小时,即2019-07-10 11:30)

如果你的业务里要求起始时间是次日的固定早间时段(比如例子里的09:30),可以直接调整起始时间的计算逻辑,我会在代码里标注修改点。

步骤2:编写过滤逻辑

接下来把每条数据的date和time_slot拼接成完整的日期时间对象,判断它是否在我们设定的时间范围内,同时确保available状态为"1"(可用)。

完整代码示例

// 模拟当前时间(实际使用时替换成 new Date() 获取真实当前时间)
const now = new Date("2019-07-08T11:30:00");

// 计算起始时间:次日的当前时间
const startDate = new Date(now);
startDate.setDate(startDate.getDate() + 1);
// 如果需要改成次日固定时间(比如例子里的09:30),取消下面两行注释:
// startDate.setHours(9, 30, 0, 0);

// 计算结束时间:再次日的当前时间(起始时间+24小时)
const endDate = new Date(startDate);
endDate.setDate(endDate.getDate() + 1);

// 你的原始数据
const dataDB = [ 
  {ID: "404", date: "2019-07-08", time_slot: "09:30:00", available: "1"}, 
  {ID: "405", date: "2019-07-08", time_slot: "10:00:00", available: "1"}, 
  {ID: "406", date: "2019-07-08", time_slot: "10:30:00", available: "1"}, 
  {ID: "407", date: "2019-07-08", time_slot: "11:00:00", available: "1"}, 
  {ID: "408", date: "2019-07-08", time_slot: "11:30:00", available: "1"}, 
  {ID: "414", date: "2019-07-09", time_slot: "09:30:00", available: "1"}, 
  {ID: "415", date: "2019-07-09", time_slot: "10:00:00", available: "1"}, 
  {ID: "416", date: "2019-07-09", time_slot: "10:30:00", available: "1"}, 
  {ID: "417", date: "2019-07-09", time_slot: "11:00:00", available: "1"}, 
  {ID: "418", date: "2019-07-09", time_slot: "11:30:00", available: "1"}, 
  {ID: "424", date: "2019-07-10", time_slot: "09:30:00", available: "1"}, 
  {ID: "425", date: "2019-07-10", time_slot: "10:00:00", available: "1"}, 
  {ID: "426", date: "2019-07-10", time_slot: "10:30:00", available: "1"}, 
  {ID: "427", date: "2019-07-10", time_slot: "11:00:00", available: "0"}, 
  {ID: "432", date: "2019-07-10", time_slot: "14:15:00", available: "1"}, 
  {ID: "433", date: "2019-07-10", time_slot: "14:45:00", available: "1"}, 
  {ID: "434", date: "2019-07-11", time_slot: "09:30:00", available: "1"}, 
  {ID: "435", date: "2019-07-11", time_slot: "10:00:00", available: "1"}, 
  {ID: "438", date: "2019-07-11", time_slot: "11:30:00", available: "1"}, 
  {ID: "439", date: "2019-07-11", time_slot: "12:00:00", available: "1"} 
];

// 过滤符合条件的时段
const filteredSlots = dataDB.filter(slot => {
  // 拼接日期和时间为完整的Date对象
  const slotDateTime = new Date(`${slot.date}T${slot.time_slot}`);
  // 检查是否在时间范围内,且可用
  return slotDateTime >= startDate && slotDateTime < endDate && slot.available === "1";
});

console.log(filteredSlots);

代码解释

  • 时间范围计算:使用Date对象的setDate方法增减日期,确保得到准确的次日和再次日时间。如果需要固定起始时间,额外设置小时和分钟即可。
  • 数据过滤:通过filter方法遍历每条数据,把date和time_slot拼接成标准ISO格式字符串,转换成Date对象后判断是否在时间范围内,同时检查可用状态。

测试结果(基于你的例子)

如果当前时间是2019-07-08 11:30:00:

  • 若使用次日当前时间作为起始,筛选结果会包含2019-07-09 11:30到2019-07-10 11:30之间的所有可用时段;
  • 若改成次日09:30作为起始(取消代码里的注释),结果会和你例子里的需求完全匹配,包含2019-07-09 09:30到2019-07-10 11:30之间的可用时段。

额外优化建议

  • 时区处理:如果系统涉及多时区,建议统一使用UTC时间计算,避免本地时区差异导致的错误;
  • 空结果处理:如果过滤后没有可用时段,可以保留原来的逐天递增逻辑,继续往后查找下一个24小时的时段。

内容的提问来源于stack exchange,提问作者Victor P

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 09:09:55