我正在尝试从REST API中提取一些数据,并将其与Moment结合使用以更新在线商店的开/关时间。以下是我的REST API数据的示例
"id": 1,
"store_open_close": {
"enabled": true,
"time": {
"sunday": {
"status": "open",
"opening_time": "10:00",
"closing_time": "21:00"
},
"monday": {
"status": "open",
"opening_time": "10:00",
"closing_time": "21:00"
},
"tuesday": {
"status": "open",
"opening_time": "10:00",
"closing_time": "21:00"
},
"wednesday": {
"status": "open",
"opening_time": "10:00",
"closing_time": "21:00"
},
"thursday": {
"status": "open",
"opening_time": "10:00",
"closing_time": "21:00"
},
"friday": {
"status": "open",
"opening_time": "10:00",
"closing_time": "23:00"
},
"saturday": {
"status": "open",
"opening_time": "11:00 am",
"closing_time": "22:00"
}
},
"open_notice": "We are open",
"close_notice": "Sorry we are closed"
},
"id": 2...
这是我现在使用的代码,用于手动设置打开/关闭时间
isStoreOpen() {
// Get formatted time
const current_time = new moment()
const format = "LT"
const time = moment(current_time, format),
beforeTime = moment("10:00", format),
afterTime = moment("21:00", format);
// Check if current time is between open and close times
if (time.isBetween(beforeTime, afterTime)) {
this.setState({ restaurantOpen: true })
} else {
this.setState({ restaurantOpen: false })
}
},
... 我需要类似的东西
if (restaruantstores.id === product.store.id) { isStoreOpen() { ... } }
我试图将来自REST API的数据用作beforeTime和afterTime。我能够成功显示打开/关闭时间(在Paul上个月的StackOverflow的帮助下)。
item.store_open_close.time[currentDay.toLowerCase()].opening_time
但是我试图通过REST API远程更新这些时间 任何帮助,不胜感激。谢谢!
答案 0 :(得分:0)
你可以得到类似的东西
isStoreOpen() {
// ...
const weekdays = [
'-',
'monday',
'tuesday',
'wednesday',
'thursday',
'friday',
'saturday',
'sunday',
];
const currentDay = weekdays[moment().isoWeekday()];
const storeHours = store_open_close.time[currentDay];
// now you can use any properties like status, opening_time, closing_time
// here the code of your function continues
// ...