gpt4 book ai didi

javascript - 如何查找当前时间是否存在于给定的时间段中并查找日期是否存在于给定的天数数组之间

转载 作者:行者123 更新时间:2023-12-03 23:01:35 24 4
gpt4 key购买 nike

这是我的日子 list

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_from 存在和 day_to例如这里 day_from是 5,即星期六, day_to是 2 星期三,所以所需的数组是: ["Saturday", "Sunday", "Monday". "Tuesday". "Wednesday"]如果当前时间存在于 time_from 中,则时间相同和 time_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);
}
}
}
它没有给出所需的结果。

最佳答案

我的理解是您希望将您的数组视为 circular ,然后是 slice它根据“from”和“to”索引,其中“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

关于javascript - 如何查找当前时间是否存在于给定的时间段中并查找日期是否存在于给定的天数数组之间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65402170/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com