Home » Javascript » JavaScript – Date from 9 pm to next day morning 9 am

JavaScript – Date from 9 pm to next day morning 9 am

Posted by: admin August 19, 2018 Leave a comment

Questions:

I want to set the dates between 9 P.M – next day 9 A.M.

  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;

  }

If i set like this. When i try to pass these values in condition First two Lunch and Snack works fine.But Dinner is not showing the correct results.

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

// my code
}

I want the dinner to show from previous day 10 PM to next day morning 9 AM.

Answers: