如何根据用户不同的时区和操作时间检查存储是打开还是关闭

时间:2019-01-22 05:06:20

标签: javascript jquery time timezone

如何根据opening timeclosing timetimezone检查商店是打开还是关闭。目前,我在服务器端使用的代码用于检查存储是否仍在打开或关闭,但是我一直在尝试用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");

1 个答案:

答案 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"))