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

如何在Node.js中根据忙碌时段查找≥60分钟的当日空闲时段

Node.js: Find 60+ Minute Free Slots in a Workday

Got it, let's tackle this problem efficiently with minimal Node.js code. Here's a concise solution that handles the work hours, busy slots, and filters for free slots of 60+ minutes:

Approach

  1. Convert all time values to Unix timestamps for easy arithmetic (no messy date string comparisons).
  2. Ensure busy slots are sorted by start time—this handles edge cases where slots might be out of order.
  3. Calculate gaps in three parts:
    • From work start to the first busy slot
    • Between consecutive busy slots
    • From the last busy slot to work end
  4. Filter gaps that are 60+ minutes long, then convert back to ISO 8601 strings for consistency.

Solution Code

// Define work hours and busy slots
const workingStartTime = new Date('2018-05-12T08:00:00.000Z').getTime();
const workingEndTime = new Date('2018-05-12T22:00:00.000Z').getTime();
const busySlots = [
  { start_time: '2018-05-12T14:30:00.000Z', end_time: '2018-05-12T17:45:00.000Z' },
  { start_time: '2018-05-12T20:30:00.000Z', end_time: '2018-05-12T21:45:00.000Z' }
];

// Convert busy slots to timestamps and sort by start time
const sortedBusy = busySlots
  .map(slot => ({
    start: new Date(slot.start_time).getTime(),
    end: new Date(slot.end_time).getTime()
  }))
  .sort((a, b) => a.start - b.start);

// Calculate valid free slots
const freeSlots = [];
let prevEnd = workingStartTime;

// Check gaps between work start and busy slots, plus between slots
for (const slot of sortedBusy) {
  if (slot.start > prevEnd) {
    const duration = (slot.start - prevEnd) / (1000 * 60); // Convert ms to minutes
    if (duration >= 60) {
      freeSlots.push({
        start_time: new Date(prevEnd).toISOString(),
        end_time: new Date(slot.start).toISOString()
      });
    }
  }
  prevEnd = Math.max(prevEnd, slot.end); // Update to latest end time
}

// Check gap between last busy slot and work end
if (workingEndTime > prevEnd) {
  const duration = (workingEndTime - prevEnd) / (1000 * 60);
  if (duration >= 60) {
    freeSlots.push({
      start_time: new Date(prevEnd).toISOString(),
      end_time: new Date(workingEndTime).toISOString()
    });
  }
}

console.log(freeSlots);

Output

Running this code will return the valid 60+ minute free slots:

[
  {
    start_time: '2018-05-12T08:00:00.000Z',
    end_time: '2018-05-12T14:30:00.000Z'
  },
  {
    start_time: '2018-05-12T17:45:00.000Z',
    end_time: '2018-05-12T20:30:00.000Z'
  }
]

Key Notes

  • Uses only Node.js built-in methods—no external dependencies needed, keeping it lightweight.
  • Sorting ensures we handle unsorted busy slot input correctly (a common real-world edge case).
  • Timestamp arithmetic avoids date parsing errors and makes duration checks straightforward.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 06:43:43