javascript - 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 点。
解决方案
我认为您应该使用 Date 而不是 hours
mindate=date;
mindate=mindate.setHour(22);
maxdate=date;
maxdate=maxdate.setDate(maxdate.getDate()+1).setHour(9);
if (mindate>x && x<maxdate)
日期是当前日期
推荐阅读
- bash - 启动并终止脚本中的程序并继续
- javascript - Visual Studio:在嵌入式 babel 脚本中自动完成 HTML 标记
- linux - 如何在 dyntick 模式下卸载 1HZ 滴答?
- javascript - Openlayers:将点列表添加到图层
- java - Java 程序从控制台读取字符串并打印其中包含最大字数的字符串
- git - Simple-git 在每个 git 命令上请求用户名和密码
- c++ - 创建消息接收侦听器时发生 Socket.IO 错误
- json - 如何比较 JOLT 转换中的字段值?
- css - 使用网格材质 UI 时出现响应式设计问题?
- php - 如何检查给定三个数组中的条件