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

JavaScript跨天时间区间(22点至次日9点)判断失效问题排查

Fixing Cross-Day Time Slot Check for Dinner

Ah, I see the issue here—your current logic fails for the Dinner slot because it's a cross-midnight window, and the standard >=/< check doesn't work when set_min (22) is larger than set_max (9). Let's break this down and fix it.

Why the Original Logic Breaks

When you set set_min = 22 and set_max = 9, the condition passintime >= 22 && passintime <9 can never be true. Since passintime is an hour between 0 and 23, there's no value that's both >=22 AND <9. That's why your Dinner slot isn't being detected correctly.

The Solution: Adjust the Check for Cross-Day Slots

We need to handle two cases:

  • Normal non-cross-day slots (like Lunch and Snack): Use the original >= and < logic.
  • Cross-day slots (Dinner here): Check if the time is either >= the start time (22) OR < the end time (9).

Here's how to update your check code:

var passintime = new Date(data[i].passintime).getHours();
var isInTargetSlot;

// Check if we're dealing with a cross-day slot
if (set_min > set_max) {
  // Cross-day: time is >= start OR < end
  isInTargetSlot = passintime >= set_min || passintime < set_max;
} else {
  // Normal slot: time is between start (inclusive) and end (exclusive)
  isInTargetSlot = passintime >= set_min && passintime < set_max;
}

if (isInTargetSlot) {
  // Your business code here
}

Alternative: One-Liner Condition

If you prefer a more concise version without the extra variable, you can combine both cases into a single condition:

var passintime = new Date(data[i].passintime).getHours();

if (
  (set_min <= set_max && passintime >= set_min && passintime < set_max) ||
  (set_min > set_max && (passintime >= set_min || passintime < set_max))
) {
  // Your business code here
}

Test Cases to Verify

Let's make sure this works for Dinner slot:

  • ✅ 22, 23, 0, 1, ..., 8: All return true (correctly in Dinner slot)
  • ❌ 9, 10, ..., 17, 18, ..., 21: Return false (correctly excluded from Dinner)

Your Lunch and Snack slot checks will continue to work as before since their set_min is less than set_max, so the original logic applies.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 07:23:07