如何比较给定时间,落在特定时间范围之间 - javascript

Posted

技术标签:

【中文标题】如何比较给定时间,落在特定时间范围之间 - javascript【英文标题】:how to compare a given time, falls between a specific time range - javascript 【发布时间】:2018-08-17 06:19:44 【问题描述】:

我正在使用节点 js 为时尚相关的应用程序创建后端,我需要显示特定商店是根据当前时间打开或关闭的。那么,如何比较当前时间与商店的开始结束时间?任何帮助,将不胜感激。下面给出了存储在db中的时序数据集:

"timings" : [
    
      "id": 1,
      "day": "Monday",
      "session_one": 
        "start_time": "10:00 AM",
        "end_time": "02:00 PM"
      ,
      "session_two": 
        "start_time": "04:00 PM",
        "end_time": "09:00 PM"
      ,
      "session_three": 
        "start_time": "03:00 PM",
        "end_time": "07:30 PM"
      
    ,
    
      "id": 2,
      "day": "Tuesday",
      "session_one": 
        "start_time": "10:00 AM",
        "end_time": "02:00 PM"
      ,
      "session_two": 
        "start_time": "04:00 PM",
        "end_time": "09:00 PM"
      ,
      "session_three": 
        "start_time": "03:00 PM",
        "end_time": "07:30 PM"
      
    ,
    
      "id": 3,
      "day": "Wednesday",
      "session_one": 
        "start_time": "10:00 AM",
        "end_time": "02:00 PM"
      ,
      "session_two": 
        "start_time": "04:00 PM",
        "end_time": "09:00 PM"
      ,
      "session_three": 
        "start_time": "03:00 PM",
        "end_time": "07:30 PM"
      
    ,
    
      "id": 4,
      "day": "Thursday",
      "session_one": 
        "start_time": "10:00 AM",
        "end_time": "02:00 PM"
      ,
      "session_two": 
        "start_time": "04:00 PM",
        "end_time": "09:00 PM"
      ,
      "session_three": 
        "start_time": "03:00 PM",
        "end_time": "07:30 PM"
      
    ,
    
      "id": 5,
      "day": "Friday",
      "session_one": 
        "start_time": "10:00 AM",
        "end_time": "02:00 PM"
      ,
      "session_two": 
        "start_time": "04:00 PM",
        "end_time": "09:00 PM"
      ,
      "session_three": 
        "start_time": "03:00 PM",
        "end_time": "07:30 PM"
      
    ,
    
      "id": 6,
      "day": "Saturday",
      "session_one": 
        "start_time": "10:00 AM",
        "end_time": "02:00 PM"
      ,
      "session_two": 
        "start_time": "04:00 PM",
        "end_time": "09:00 PM"
      ,
      "session_three": 
        "start_time": "03:00 PM",
        "end_time": "07:30 PM"
      
    ,
    
      "id": 7,
      "day": "Sunday",
      "session_one": 
        "start_time": "10:00 AM",
        "end_time": "02:00 PM"
      ,
      "session_two": 
        "start_time": "04:00 PM",
        "end_time": "09:00 PM"
      ,
      "session_three": 
        "start_time": "03:00 PM",
        "end_time": "07:30 PM"
      
    
  ]

【问题讨论】:

你的意思是在获取数据之前检查范围? Mongodb 对此有明确提及 看看docs.mongodb.com/manual/reference/operator/aggregation/hour 看看这个***.com/questions/11973304/… 是的,在将其发送给客户端之前,我需要检查请求的时间是否介于任何会话时间之间。 是的,完全是@VikasValechha,你必须在服务器端检查它 我看到了这个***.com/questions/11973304/…,但在我的情况下,日期存储在字符串中而不是日期对象中,那么我该如何比较它们呢? @SaikatHajra 【参考方案1】:

试试这个,

function timeCal(time) 
    let timeSplit = time.split(' ');
    let timeInMinutes = 0;

    if(timeSplit[1] === 'AM') 
        let timeArr = timeSplit[0].split(':');

        let hours = parseInt(timeArr[0]);
        let minutes = parseInt(timeArr[1]); 

        timeInMinutes = (hours === 12) ? minutes : (hours * 60) + minutes;

     else if(timeSplit[1] === 'PM') 
        let timeArr = timeSplit[0].split(':');

        let hours = parseInt(timeArr[0]);
        let minutes = parseInt(timeArr[1]); 

        timeInMinutes = (hours === 12) ? (hours * 60 + minutes) : ((hours + 12) * 60) + minutes;
    

    return timeInMinutes;



let time =  
        "start_time": "10:20 AM",
        "end_time": "02:00 PM"
      ;

let start = timeCal(time.start_time);
let end = timeCal(time.end_time);
let today = new Date();
let current = today.getHours() * 60 + today.getMinutes();

(start<=current && end>=current) ? console.log('open') : console.log('closed');

【讨论】:

以上是关于如何比较给定时间,落在特定时间范围之间 - javascript的主要内容,如果未能解决你的问题,请参考以下文章

48 python判断时间是否落在两个时区之间(只比较时刻不比较日期)

如果有任何坐标落在彼此之间的特定距离内,我如何确定哪些坐标?

给定'getrand100()'函数获取特定范围内的随机数

当时间范围在两天之间时如何获取特定时间范围内的记录

搜索给定范围之间的日期 - Lotus

如何查看IP网络块?