我想要做的是:
getOpenStatus = (restaurant: _Restaurant) => {
const closeHour = moment(restaurant.close_at, "HH:mm A").hours();
const closeMin = moment(restaurant.close_at, "HH:mm A").minutes();
const openHour = moment(restaurant.open_at, "HH:mm A").hours();
const openMin = moment(restaurant.open_at, "HH:mm A").minutes();
const closeMoment = moment({ hours: closeHour, minutes: closeMin });
const openMoment = moment({ hours: openHour, minutes: openMin });
return moment().isAfter(openMoment) && moment().isBefore(closeMoment);
}假设当前时间是下午4:00
上午10:30开放,晚上11:30关闭
在这种情况下,因为时间在同一日期,所以可以完美地工作。
但是如果餐厅23小时营业呢:
上午10:30开放,上午9:30关闭
那么该如何处理呢?
发布于 2019-06-02 22:28:30
getOpenStatus = (restaurant: _Restaurant) => {
const closeHour = moment(restaurant.close_at, "HH:mm A").hours();
const closeMin = moment(restaurant.close_at, "HH:mm A").minutes();
const openHour = moment(restaurant.open_at, "HH:mm A").hours();
const openMin = moment(restaurant.open_at, "HH:mm A").minutes();
const closeMoment = moment({ hours: closeHour, minutes: closeMin });
const openMoment = moment({ hours: openHour, minutes: openMin });
if (closeMoment.isBefore(openMoment)) {
if (moment().isAfter(closeMoment)) closeMoment.add(1, "days");
else openMoment.subtract(1, "days");
}
return moment().isAfter(openMoment) && moment().isBefore(closeMoment);
}https://stackoverflow.com/questions/56415269
复制相似问题