gpt4 book ai didi

javascript - 如何使用 moment.js 和 moment timezone 确定商店是打开还是关闭?

转载 作者:行者123 更新时间:2023-11-30 19:01:18 24 4
gpt4 key购买 nike

我在我的网站中使用 momentjs 来根据特定商店位置的选定开门、关门时间和时区来确定商店是仍在营业还是关门。我制作的函数在某些时间范围内有效,但在某些时间范围内无效。

JS 库

/moment.js
/moment-timezone-with-data-10-year-range.js

我的示例代码

 function isOpen(openTime, closeTime, timezone){
var status = "closed";
if(openTime != "24HR"){
const now = moment().tz(timezone);
const storeOpenTime = moment.tz(openTime, "h:mmA", timezone);
const storeCloseTime = moment.tz(closeTime, "h:mmA", timezone);
/* const storeOpenTime = moment.tz(Date.now(Date(openTime)), "h:mmA", timezone);
const storeCloseTime = moment.tz(Date.now(Date(closeTime)), "h:mmA", timezone);*/
const check = now.isBetween(storeOpenTime, storeCloseTime);
if(check || check == true){
status = "open";
}
}else{
status = "open";
}
return status;
}

假设马来西亚吉隆坡的当前时间是晚上 11:34,我运行以下代码

 isOpen("8:00AM", "12:20AM", "Asia/Kuala_Lumpur") 
returned output = closed

上面代码的预期输出是open,但是如果在马来西亚吉隆坡的当前时间是晚上 11:34 时运行下面的代码。

isOpen("8:00AM", "10:20PM", "Asia/Kuala_Lumpur")
returned output = closed

上面的输出是正确的。

我的问题是如何使用 momentjs 检查打开和关闭之间的时间是否已使用国家时区?

最佳答案

这是您的代码更正和注释:

function isOpen(openTime, closeTime, timezone) {

// handle special case
if (openTime === "24HR") {
return "open";
}

// get the current date and time in the given time zone
const now = moment.tz(timezone);

// Get the exact open and close times on that date in the given time zone
// See https://github.com/moment/moment-timezone/issues/119
const date = now.format("YYYY-MM-DD");
const storeOpenTime = moment.tz(date + ' ' + openTime, "YYYY-MM-DD h:mmA", timezone);
const storeCloseTime = moment.tz(date + ' ' + closeTime, "YYYY-MM-DD h:mmA", timezone);

let check;
if (storeCloseTime.isBefore(storeOpenTime)) {
// Handle ranges that span over midnight
check = now.isAfter(storeOpenTime) || now.isBefore(storeCloseTime);
} else {
// Normal range check using an inclusive start time and exclusive end time
check = now.isBetween(storeOpenTime, storeCloseTime, null, '[)');
}

return check ? "open" : "closed";
}

// Testing
const zone = "Asia/Kuala_Lumpur";
console.log("now", moment.tz(zone).format("h:mmA"));
console.log("24HR", isOpen("24HR", undefined, zone));
console.log("2:00AM-8:00AM", isOpen("2:00AM", "8:00AM", zone));
console.log("8:00AM-2:00PM", isOpen("8:00AM", "2:00PM", zone));
console.log("2:00PM-8:00PM", isOpen("2:00PM", "8:00PM", zone));
console.log("8:00PM-2:00AM", isOpen("8:00PM", "2:00AM", zone));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.27/moment-timezone-with-data-10-year-range.min.js"></script>

您遇到的主要问题是您必须解决 issue #119 中描述的错误,即在特定时区解析时间时,Moment 错误地应用了 UTC 日期而不是特定于 tz 的本地日期。

其次,您还需要通过检查打开/关闭时刻是否乱序来处理时间范围跨越午夜的情况。如果是,则需要以不同的方式进行比较 - 通过检查当前时间是在打开之后还是关闭之前。

关于javascript - 如何使用 moment.js 和 moment timezone 确定商店是打开还是关闭?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59490774/

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