如何根据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");
答案 0 :(得分:1)
我建议为此使用moment-timezone。
您应该可以执行以下操作:
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"))