简体   繁体   English

如何在Javascript中检查选定的时间是否在给定的时间范围内

[英]How to check selected time is within the given time range in Javascript

I need to determine if the selected times (start time, end time) by the user is in the array of appointment times.我需要确定用户选择的时间(开始时间、结束时间)是否在约会时间数组中。

I get the appointment times from exchange web service and the output is :我从交换网络服务获得约会时间,输出是:

[{Status: "Busy", StartTime: "2016-10-05T11:00:00+10:00", EndTime: "2016-10-05T11:30:00+10:00"},{Status: "Busy", StartTime: "2016-10-05T13:00:00+10:00", EndTime: "2016-10-05T15:30:00+10:00"}]

The times that user has selected are :用户选择的时间是:

var start_time = Date.parse("2016-10-05T14:30");
var end_time = Date.parse("2016-10-05T15:00");

So far I have tried the following but I am not sure if this is the correct way to do it:到目前为止,我已经尝试了以下方法,但我不确定这是否是正确的方法:

var data = [{Status: "Busy", StartTime: "2016-10-05T11:00:00+10:00", EndTime: "2016-10-05T11:30:00+10:00"},{Status: "Busy", StartTime: "2016-10-05T13:00:00+10:00", EndTime: "2016-10-05T15:30:00+10:00"}];

for (var key in data)
{
  var obj = data[key];
  if (obj['Status'] == 'Busy')
  {
    check_open_time_val = Date.parse(obj['StartTime']);
    check_close_time_val = Date.parse(obj['EndTime']);

     if (check_open_time_val > open_time)
     {
        console.log('Is Busy');
        // break;
        return true;
      }
   }else {
      console.log(obj['Status'] + 'XXXXX');
   }
  }

How do I check if the start_time and the endtime are not in any of the appointment times.如何检查 start_time 和 endtime 是否不在任何约会时间。 Thanks谢谢

You can combine this logic with Array.prototype.some您可以将此逻辑Array.prototype.some结合使用

 let data = [{Status: "Busy", StartTime: "2016-10-05T11:00:00+10:00", EndTime: "2016-10-05T11:30:00+10:00"},{Status: "Busy", StartTime: "2016-10-05T13:00:00+10:00", EndTime: "2016-10-05T15:30:00+10:00"}], start_time = Date.parse("2016-10-05T14:30:00+10:00"), end_time = Date.parse("2016-10-05T15:00:00+10:00"); let isClash = data.some(obj => { let s = Date.parse(obj.StartTime), e = Date.parse(obj.EndTime); return obj.Status === 'Busy' && start_time <= e && end_time >= s; }); console.log('is clash?', isClash);

Note that I aligned the UTC offsets of your start_time and end_time so you could see that it works :)请注意,我对齐了start_timeend_time的 UTC 偏移量,因此您可以看到它有效:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM