【问题标题】:How find if the current time exist in give time slots and find if the day is exist between given array of days如何查找当前时间是否存在于给定的时间段中并查找日期是否存在于给定的天数数组之间
【发布时间】:2020-12-22 01:31:43
【问题描述】:

这是我的日程安排

days_list: any[] = [
    {
      id: 0,
      value: 'Monday'
    },
    {
      id: 1,
      value: 'Tuesday'
    }, {
      id: 2,
      value: 'Wednesday'
    }, {
      id: 3,
      value: 'Thursday'
    }, {
      id: 4,
      value: 'Friday'
    }, {
      id: 5,
      value: 'Saturday'
    },
    {
      id: 6,
      value: 'Sunday'
    },
  ]

这是我的营业时间

business_hours = { day_to: 2, time_to: "23:00", day_from: 5, time_from: "08:00" }

我正在使用 UTC 日期格式我想知道 days_list 的日期是否根据 day_fromday_to 存在

例如,day_from 是 5,即星期六,day_to 是 2 星期三,因此所需的数组是: ["Saturday", "Sunday", "Monday". "Tuesday". "Wednesday"] 如果当前时间存在于time_fromtime_to 中,则时间相同,

我的代码是:

   const activationDate = new Date();

    const d_date = activationDate.getUTCDay() - 1;
    console.log(d_date);

    const B_from = this.getMin(this.business_hours.time_from);

    const B_To = this.getMin(this.business_hours.time_to);


    const min = activationDate.getUTCMinutes();
    console.log(min)
    const naan = activationDate.getUTCHours();
    console.log(naan)
    const utcTime = this.getUtcMin(naan, min);



    for(let j = 0; j < this.business_hours.day_to; j++) {
    for (let i = this.business_hours.day_from; i < this.days_list.length; i++) {
     
      console.log(this.days_list[i]);

      if (this.days_list[i].id === d_date) {
        this.is_open = true;
        console.log(this.days_list[i].value);
      }
     }
    }

它没有给出所需的结果。

【问题讨论】:

标签: javascript angular typescript


【解决方案1】:

我的理解是您希望将您的数组视为circular,然后根据“from”和“to”索引处理slice,其中“from”和“to”索引都被处理作为inclusive

假设你有一个这样的字符串数组:

console.log(dayArray);
// ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] 

(你可以很容易地把你的结构变成这样:

const dayArray = days_list.reduce<string[]>((a, d) => (a[d.id] = d.value, a), []);

)

然后,您可以通过多种方式编写具有包含端点函数的圆形数组切片。这是一个:

function circularArraySlice<T>(arr: T[], fromIndex: number, toIndex: number) {
  return arr.map((_, i, a) => a[(i + fromIndex) % a.length]).
    slice(0, ((arr.length + toIndex - fromIndex) % arr.length) + 1);
}

本质上,我们使用modular arithmetic(几乎)由JS remainder operator (%) 实现,从数组的末尾走出来并回到开头。让我们看看它是否有效:

console.log(circularArraySlice(dayArray, 5, 2));
// ["Saturday", "Sunday", "Monday", "Tuesday", "Wednesday"] 

console.log(circularArraySlice(dayArray, 2, 5));
// ["Wednesday", "Thursday", "Friday", "Saturday"]

我想,这就是你想要的。很可能有边缘情况,所以要小心。

Playground link to code

【讨论】:

    【解决方案2】:

    为了帮助回答此类问题,我建议列出几个测试用例,它们都提供预期值和您实际看到的值。

    但我可以看到一些可能导致代码出现问题的事情:

    • d_date 的计算将在周日返回 -1,而不是 6(就像 days_list 预期的那样)
    • 外部循环(一个设置j)在这里并没有真正增加很多,因为j 没有在循环内部使用。因此,循环的每次迭代都会产生相同的效果。
    • 内部循环(一个设置i)仅在days_list 数组中查找出现在day_from 之后的天数。但是,根据您的示例,如果day_from 值大于day_to,则从days_list 开始的天数也可能匹配。

    【讨论】:

      【解决方案3】:

      根据Randy Casburn(现已删除)的答案,可以使用javascript 的filter 方法解决此问题。 但是,当 to_datefrom_date 之前时,您需要特别小心处理不同的情况,反之亦然。

      例如:

      const days_list = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
      
      function getDays(business_hours) {
        const days = days_list.filter((_, id) => {
            if (business_hours.day_from <= business_hours.day_to) {
              return (id >= business_hours.day_from && id <= business_hours.day_to);
            } else {
              return (id <= business_hours.day_to || id >= business_hours.day_from);
            }
        })
        console.log(business_hours, days);
        return days;
      }
      getDays({ day_from: 2, time_from: "23:00", day_to: 5, time_to: "08:00"});
      getDays({ day_to: 2, time_to: "23:00", day_from: 5, time_from: "08:00"});
      getDays({ day_from: 3, time_from: "23:00", day_to: 3, time_to: "08:00"});

      【讨论】:

        猜你喜欢
        • 2012-02-25
        • 1970-01-01
        • 1970-01-01
        • 2018-07-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-03-06
        • 1970-01-01
        相关资源
        最近更新 更多