首页 > 解决方案 > JavaScript - 日期从晚上 9 点到第二天早上 9 点

问题描述

我想在晚上 9 点到第二天早上 9 点之间设置日期

  var set_min;
  var set_max;
  if ( hours >= 9 && hours <= 17 ) // 9 AM to 5 PM
  {
      day_part = "Lunch";
       set_min = 9;
       set_max = 17;      

  }
  else if (hours >= 18 && hours <= 21 ) // 6 PM to 9 PM
  {
      day_part = "Snack";
       set_min = 18; 
       set_max = 21;
  }
  else
  {
      day_part = "Dinner";

// Here i want to make it from 10 PM to 9 AM Next day
       set_min = 22;
       set_max = 9;

  }

如果我这样设置。当我尝试在前两个午餐和小吃的条件下传递这些值时,工作正常。但晚餐没有显示正确的结果。

     var passintime = new Date(data[i].passintime).getHours(); 
     if (passintime >=set_min && passintime < set_max)
{

// my code
}

我希望晚餐从前一天晚上 10 点显示到第二天早上 9 点。

标签: javascriptnode.jsajax

解决方案


我认为您应该使用 Date 而不是 hours

mindate=date;
mindate=mindate.setHour(22);
maxdate=date;
maxdate=maxdate.setDate(maxdate.getDate()+1).setHour(9);
if (mindate>x && x<maxdate)

日期是当前日期


推荐阅读