如何根据opening time
、closing time
和timezone
检查商店是打开还是关闭。目前,我有在服务器端使用的代码来检查商店是否仍然打开或关闭,但我一直在尝试在javascript或jQuery中制作相同的功能,但没有运气。
function OperationHours($open, $close, $timezone = "GMT"){
$status = 'closed';
$nowTime = new DateTime("NOW", new DateTimeZone($timezone));
$openTime = DateTime::createFromFormat('h:iA', $open, new DateTimeZone($timezone));
$closeTime = DateTime::createFromFormat('h:iA', $close, new DateTimeZone($timezone));
// check if the close time is before the opening time
if($closeTime <= $openTime){
$closeTime->add(new DateInterval("P1D"));
}
if ($nowTime > $openTime && $nowTime < $closeTime){
$status = 'open';
}
return $status;
}
echo OperationHours("7:00AM", "10:30PM", "Asia/Kuala_Lumpur");
我建议为此使用时刻时区。
你应该能够做这样的事情:
const isOpen = (openTime, closeTime, timezone) => {
const now = moment().tz(timezone);
const storeOpenTime = moment.tz(openTime, "h:mmA", timezone);
const storeCloseTime = moment.tz(closeTime, "h:mmA", timezone);
return now.isBetween(storeOpenTime, storeCloseTime);
}
console.log(isOpen("7:00AM", "10:30PM", "Asia/Kuala_Lumpur"))